这里把三个对话框形式写在一个项目程序里面,用三个按钮控制显示什么样式的对话框。
先看布局文件代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定取消对话框" android:onClick="click"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="单选对话框" android:onClick="click2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="多选对话框" android:onClick="click3"/> </LinearLayout>
配置文件和之前写的配置方式一样。
看MainActivity总的代码:
package com.example.dialog; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //这里是确定取消对话框 public void click(View v){ AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setIcon(android.R.drawable.alert_dark_frame); dialog.setTitle("欲练此功必先自宫"); dialog.setCancelable(false); dialog.setMessage("东方教主,一统江湖!"); //设置确定按钮 dialog.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "感谢使用,再见", 0).show(); } }); //设置取消按钮 dialog.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "若不自宫,一定不成功", 0).show(); } }); //让对话框显示 dialog.show(); } //设置单选 public void click2(View v){ AlertDialog.Builder builder = new Builder(this); builder.setTitle("请选择性别"); final String[] items = new String[]{ "男", "女" }; builder.setSingleChoiceItems(items, -1, new OnClickListener() { //which:用户所选条目下标 //dialog:出发这个方法的该对话框实例对象 @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "您的选择是:"+items[which], 0).show(); dialog.dismiss(); } }); builder.show(); } //设置多选对话框 public void click3(View v){ AlertDialog.Builder builder = new Builder(this); builder.setTitle("请选择您觉得帅的人"); final String[] items = new String[]{ "侃哥", "找帅哥", "杨道龙", "小杨" }; //设置选中第几个选项 final boolean[] checkedItems = new boolean[] { true, true, false, false }; builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { checkedItems[which] = isChecked; } }); builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String text = ""; for (int i = 0; i < 4; i++) { text += checkedItems[i] ? items[i] + "," : "";//吐丝显示选中选项 } Toast.makeText(MainActivity.this, text, 0).show(); dialog.dismiss(); } }); builder.show(); } }