AlertDialog提示对话框练习
闲来无事,就练习了下AlertDialog对话框。
首先写了三个button,分别打开一般对话框,单选列表对话框和复选对话框。
xml代码
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context=".MainActivity" 6 android:orientation="vertical"> 7 8 <Button 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="提示对话框" 12 android:onClick="listener1"/> 13 14 <Button 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:text="单选对话框" 18 android:onClick="listener2"/> 19 <Button 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:text="复选对话框" 23 android:onClick="listener3"/> 24 </LinearLayout>
java代码:
1 package com.dj.alertdialogtest; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.app.AlertDialog; 6 import android.app.AlertDialog.Builder; 7 import android.app.Dialog; 8 import android.content.DialogInterface; 9 import android.view.Menu; 10 import android.view.View; 11 import android.widget.Button; 12 import android.widget.Toast; 13 14 public class MainActivity extends Activity { 15 AlertDialog.Builder dialog=null; 16 private int num; 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 } 23 24 25 /**显示对话框*/ 26 public void listener1(View v){ 27 dialog=new AlertDialog.Builder(MainActivity.this);//初始化显示对话框 28 dialog.setIcon(R.drawable.ic_launcher);//设置对话框的图标 29 dialog.setTitle("调查");//设置对话框的标题 30 dialog.setMessage("假如给你一百万你想做什么?");//设置对话框的内容信息 31 32 /**设置对话的三个button按钮*/ 33 dialog.setPositiveButton("捐款",new DialogInterface.OnClickListener() { 34 @Override 35 public void onClick(DialogInterface dialog, int which) { 36 Toast.makeText(getApplicationContext(), "我要做有意思的事!", Toast.LENGTH_LONG).show(); 37 } 38 }); 39 dialog.setNegativeButton("旅游", new DialogInterface.OnClickListener() { 40 41 @Override 42 public void onClick(DialogInterface dialog, int which) { 43 Toast.makeText(getApplicationContext(), "我要好好活!", Toast.LENGTH_LONG).show(); 44 } 45 }); 46 47 dialog.setNeutralButton("坐吃等死", new DialogInterface.OnClickListener() { 48 49 @Override 50 public void onClick(DialogInterface dialog, int which) { 51 Toast.makeText(getApplicationContext(), "我要坐吃等死!", Toast.LENGTH_LONG).show(); 52 } 53 }); 54 dialog.create();//创建 55 dialog.show();//将设置完的对话框显示出来 56 } 57 58 59 60 /**以下是单选列表对话框*/ 61 public void listener2(View v){ 62 AlertDialog.Builder dialog=new AlertDialog.Builder(this); 63 final String[] str={"鱼香肉丝","宫保鸡丁","回锅肉","东坡肉"};//设置列表的元素 64 dialog.setIcon(R.drawable.ic_launcher);//设置图标 65 dialog.setTitle("选择你喜欢的食物");//设置标题 66 /**设置单选列表对话框。第一个参数为列表元素,以字符串数组承装; 67 * 第二个参数是默认选中的元素,0表示第一个*/ 68 dialog.setSingleChoiceItems(str, 0, new DialogInterface.OnClickListener() { 69 @Override 70 public void onClick(DialogInterface dialog, int which) { 71 num=which;//which表示哪个下标被选中 72 } 73 }); 74 dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { 75 @Override 76 public void onClick(DialogInterface dialog, int which) { 77 String temp="你喜欢的食物是:"; 78 //str[num]表示选中的是哪个食物 79 Toast.makeText(getApplicationContext(), temp+str[num], Toast.LENGTH_LONG).show(); 80 } 81 }); 82 dialog.create(); 83 dialog.show(); 84 } 85 86 87 /**复选列表对话框*/ 88 public void listener3(View v){ 89 AlertDialog.Builder dialog=new AlertDialog.Builder(this); 90 final String[] str={"王力宏","周杰伦","王菲","那英"}; 91 final boolean[] flags={true,false,true,false}; 92 dialog.setIcon(R.drawable.ic_launcher);//设置图标 93 dialog.setTitle("你喜欢的歌手是"); 94 dialog.setMultiChoiceItems(str, flags, new DialogInterface.OnMultiChoiceClickListener() { 95 96 @Override 97 public void onClick(DialogInterface arg0, int which, boolean isChecked) { 98 flags[which]=isChecked;//哪一项被选中 99 } 100 }); 101 dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { 102 103 @Override 104 public void onClick(DialogInterface dialog, int which) { 105 String temp="你喜欢的歌手是:"; 106 for (int i = 0; i < flags.length; i++) { 107 if(flags[i]){ 108 temp+=str[i]; 109 } 110 } 111 Toast.makeText(getApplicationContext(), temp, Toast.LENGTH_LONG).show(); 112 } 113 }); 114 dialog.create(); 115 dialog.show(); 116 } 117 @Override 118 public boolean onCreateOptionsMenu(Menu menu) { 119 // Inflate the menu; this adds items to the action bar if it is present. 120 getMenuInflater().inflate(R.menu.main, menu); 121 return true; 122 } 123 124 }
演示效果: