示例:
一、确定对话框
1 AlertDialog.Builder builder = new AlertDialog.Builder(this); 2 builder.setTitle("确认对话框"); 3 builder.setIcon(R.drawable.icon_72); 4 builder.setMessage("这里是对话框内容"); 5 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { 6 7 @Override 8 public void onClick(DialogInterface arg0, int arg1) { 9 // TODO Auto-generated method stub10 Toast.makeText(AlertDialog_text.this, "点击了确定按钮", 1).show();11 }12 });13 AlertDialog dialog = builder.create();14 dialog.show(); //显示、
二、普通列表
1 final String[] items = new String[]{ "语文","数学","英语","物理","化学"}; //列表项 2 Builder alertdialog = new AlertDialog.Builder(this); 3 alertdialog.setTitle("你喜欢的课程").setItems(items, new DialogInterface.OnClickListener() { 4 5 @Override 6 public void onClick(DialogInterface dialog, int which) { 7 // TODO Auto-generated method stub 8 Toast.makeText(AlertDialog_lianxi.this, items[which], Toast.LENGTH_SHORT).show(); 9 10 }11 });12 alertdialog.create().show(); //创建显示列表
三、单选列表
1 final String[] items_fruit = new String[]{ "苹果","香蕉","橘子","西瓜","梨"}; 2 Builder alerdialog = new AlertDialog.Builder(this); 3 //设置列表标题 4 alerdialog.setTitle("你喜欢的水果"); 5 //设置单选列表 6 alerdialog.setSingleChoiceItems(items_fruit, 0, new DialogInterface.OnClickListener() { 7 @Override 8 public void onClick(DialogInterface dialog, int which) { 9 // TODO Auto-generated method stub10 Toast.makeText(AlertDialog_lianxi.this, items_fruit[which], Toast.LENGTH_SHORT).show();11 12 }13 });14 //设置取消按钮并且设置响应事件15 alerdialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {16 17 @Override18 public void onClick(DialogInterface dialog, int which) {19 // TODO Auto-generated method stub20 //取消按钮响应事件21 }22 });23 //添加确定按钮 并且设置响应事件24 alerdialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { 25 26 @Override27 public void onClick(DialogInterface dialog, int which) {28 // TODO Auto-generated method stub29 //确定按钮响应事件30 }31 });32 33 alerdialog.create().show();//创建显示列表
四、多选列表
1 final String[] items_fruit1 = new String[]{ "苹果","香蕉","橘子","西瓜","梨"}; //设置项 2 final boolean[] items_fruit_selected = new boolean[]{ true,false,false,false,false}; 3 Builder alerdialog1 = new AlertDialog.Builder(this); 4 //设置列表标题 5 alerdialog1.setTitle("你喜欢的水果"); 6 //设置多选列表 7 alerdialog1.setMultiChoiceItems(items_fruit1, items_fruit_selected, new DialogInterface.OnMultiChoiceClickListener() { 8 9 @Override10 public void onClick(DialogInterface dialog, int which, boolean isChecked) {11 // TODO Auto-generated method stub12 items_fruit_selected[which] = isChecked;13 }14 });15 //设置取消按钮并且设置响应事件16 alerdialog1.setNegativeButton("取消", new DialogInterface.OnClickListener() {17 18 @Override19 public void onClick(DialogInterface dialog, int which) {20 // TODO Auto-generated method stub21 //取消按钮响应事件22 }23 });24 //添加确定按钮 并且设置响应事件,将选择的项显示25 alerdialog1.setPositiveButton("确定", new DialogInterface.OnClickListener() { 26 27 @Override28 public void onClick(DialogInterface dialog, int which) {29 // TODO Auto-generated method stub30 //确定按钮响应事件31 StringBuilder stringBuilder = new StringBuilder();32 for(int i=0;i
5、自定义布局对话框
对话框布局文件
1 27 8 13 14 20 25
1 package com.example.demo1; 2 3 import android.app.Activity; 4 import android.app.AlertDialog; 5 import android.content.DialogInterface; 6 import android.os.Bundle; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.View.OnClickListener;10 import android.widget.Button;11 12 public class MainActivity extends Activity {13 private Button btn_openDialog;14 private View view;15 @Override16 protected void onCreate(Bundle savedInstanceState) {17 super.onCreate(savedInstanceState);18 setContentView(R.layout.activity_main);19 btn_openDialog = (Button) findViewById(R.id.id_submit);20 btn_openDialog.setOnClickListener(new OnClickListener() {21 22 @Override23 public void onClick(View v) {24 // TODO Auto-generated method stub25 view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_dialog, null);26 AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)27 .setTitle("主题")28 .setIcon(R.drawable.ic_launcher)29 .setView(view)30 .setPositiveButton("取消", new DialogInterface.OnClickListener() {31 32 @Override33 public void onClick(DialogInterface dialog, int which) {34 // TODO Auto-generated method stub35 36 }37 })38 .create();39 dialog.show();40 }41 });42 43 44 }45 46 47 48 }