界面非常的简单,就是一个Button,点击这个Button呢,会弹出一个对话框
<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" android:orientation="vertical" tools:context=".MainActivity" > <Button android:onClick="onclick1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bt_1" /> <Button android:onClick="onclick2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bt_2" /> <Button android:onClick="onclick3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bt_3" /> </LinearLayout>
</pre><pre name="code" class="java">package com.neusoft.dialog; import junit.framework.Assert; import android.net.wifi.WifiManager.MulticastLock; import android.os.Bundle; 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.view.Menu; import android.view.MenuItem.OnMenuItemClickListener; import android.view.View; import android.widget.AbsListView.MultiChoiceModeListener; 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 onclick1(View view) //这个方法是弹出一个对话框 { AlertDialog.Builder builder=new Builder(this); builder.setIcon(R.drawable.ic_launcher);//设置图标 builder.setTitle("若想成功必先自宫");//设置对话框的标题 builder.setMessage("你确定要自宫吗?");//设置对话框的内容 builder.setPositiveButton("确定", new OnClickListener() { //这个是设置确定按钮 @Override public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(MainActivity.this, "自宫成功", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", new OnClickListener() { //取消按钮 @Override public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(MainActivity.this, "取消自宫",Toast.LENGTH_SHORT).show(); } }); AlertDialog b=builder.create(); b.show(); //必须show一下才能看到对话框,跟Toast一样的道理 } public void onclick2(View view)//这里弹出来的是一个单选对话框 { AlertDialog.Builder builder=new Builder(this); builder.setIcon(android.R.drawable.ic_dialog_info); 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], Toast.LENGTH_SHORT).show(); dialog.dismiss();//当用户选择了一个值后,对话框消失 } }); builder.show();//这样也是一个show对话框的方式,上面那个也可以 } public void onclick3(View view)//这是一个多选对话框 { AlertDialog.Builder builder=new Builder(MainActivity.this); builder.setIcon(android.R.drawable.ic_dialog_info); builder.setTitle("请选择你觉得帅的人"); final String []itemsId=new String[]{"小明","小智","小龙","小赵"}; final boolean []checkedItems=new boolean[]{true,true,false,false};//这里的true是默认第几个人已经被选中 builder.setMultiChoiceItems(itemsId, checkedItems,new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean ischeck) { checkedItems[which]=ischeck; } }); //设置一个确定按钮 builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int whick) { String text=""; for(int i=0;i<itemsId.length;i++) { text+=checkedItems[i]?itemsId[i]+",":""; } Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); builder.show(); } }
源码