示例:
一、确定对话框
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 stub 10 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 stub 10 Toast.makeText(AlertDialog_lianxi.this, items_fruit[which], Toast.LENGTH_SHORT).show(); 11 12 } 13 }); 14 //设置取消按钮并且设置响应事件 15 alerdialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { 16 17 @Override 18 public void onClick(DialogInterface dialog, int which) { 19 // TODO Auto-generated method stub 20 //取消按钮响应事件 21 } 22 }); 23 //添加确定按钮 并且设置响应事件 24 alerdialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { 25 26 @Override 27 public void onClick(DialogInterface dialog, int which) { 28 // TODO Auto-generated method stub 29 //确定按钮响应事件 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 @Override 10 public void onClick(DialogInterface dialog, int which, boolean isChecked) { 11 // TODO Auto-generated method stub 12 items_fruit_selected[which] = isChecked; 13 } 14 }); 15 //设置取消按钮并且设置响应事件 16 alerdialog1.setNegativeButton("取消", new DialogInterface.OnClickListener() { 17 18 @Override 19 public void onClick(DialogInterface dialog, int which) { 20 // TODO Auto-generated method stub 21 //取消按钮响应事件 22 } 23 }); 24 //添加确定按钮 并且设置响应事件,将选择的项显示 25 alerdialog1.setPositiveButton("确定", new DialogInterface.OnClickListener() { 26 27 @Override 28 public void onClick(DialogInterface dialog, int which) { 29 // TODO Auto-generated method stub 30 //确定按钮响应事件 31 StringBuilder stringBuilder = new StringBuilder(); 32 for(int i=0;i<items_fruit_selected.length;i++) 33 { 34 if(items_fruit_selected[i] == true) 35 { 36 stringBuilder.append(items_fruit1[i]+"、"); 37 } 38 } 39 Toast.makeText(AlertDialog_lianxi.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show(); 40 } 41 }); 42 43 alerdialog1.create().show();//创建显示列表
5、自定义布局对话框
对话框布局文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:gravity="center_horizontal" 6 android:orientation="vertical" > 7 8 <EditText 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:editable="false" 12 android:text="自定义对话框"/> 13 14 <TextView 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:text="这里是自定义对话框的内容" 18 android:textSize="20dp" 19 /> 20 <Button 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:text="确定" 24 /> 25 </LinearLayout>
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 @Override 16 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 @Override 23 public void onClick(View v) { 24 // TODO Auto-generated method stub 25 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 @Override 33 public void onClick(DialogInterface dialog, int which) { 34 // TODO Auto-generated method stub 35 36 } 37 }) 38 .create(); 39 dialog.show(); 40 } 41 }); 42 43 44 } 45 46 47 48 }