AlertDialog是Dialog的子类,要使用AlertDialog主要是熟悉AlertDialog.Builder这个类,先成成一个AlertDialog.Builder对象,通过这个对象设置AlertDialog的属性,设置完毕后调用create()方法生成。
例子中在Activity主界面中添加三个按钮,分别对应弹出三个不同风格的AlertDialog.
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/add_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="拥有按钮的AlertDialog" />
<Button
android:id="@+id/add_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="拥有表单的AlertDialog" />
<Button
android:id="@+id/add_check_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="选择项的的AlertDialog" />
</LinearLayout>
为AlertDialog添加按钮:
1 public class TestActivity extends Activity
2 {
3 /** Called when the activity is first created. */
4 @Override
5 public void onCreate(Bundle savedInstanceState)
6 {
7 super.onCreate(savedInstanceState);
8 setContentView(R.layout.main);
9
10 Button button1 = (Button) findViewById(R.id.add_button);
11 button1.setOnClickListener(new ShowAlertDialog());
12 }
13
14 // 按钮监听器
15 class ShowAlertDialog implements OnClickListener
16 {
17 public void onClick(View v)
18 {
19 //得到按钮的Id
20 int buttonId = v.getId();
21 switch (buttonId)
22 {
23 //显示拥有按钮的AlertDialog
24 case R.id.add_button:
25 AlertDialog.Builder builder = new AlertDialog.Builder(TestActivity.this);
26 builder.setMessage("确定要退出程序吗?").setPositiveButton("确定", new DialogInterface.OnClickListener()
27 {
28
29 public void onClick(DialogInterface dialog, int which)
30 {
31 TestActivity.this.finish();
32 }
33 }).setNegativeButton("取消", new DialogInterface.OnClickListener()
34 {
35
36 public void onClick(DialogInterface dialog, int which)
37 {
38 //撤销AlertDialog
39 dialog.cancel();
40 }
41 });
42 builder.create().show();
43 break;
44 }
45
46 }
47 }
48 }
程序运行结果:
为AlertDialog添加菜单:
添加的程序片段:
Button button2 = (Button) findViewById(R.id.add_list);
button2.setOnClickListener(new ShowAlertDialog());
按钮的事件处理:
case R.id.add_list:
final String[] languages =
{ "中文", "英文", "阿拉伯", "俄文" };
AlertDialog.Builder builder2 = new AlertDialog.Builder(TestActivity.this);
builder2.setTitle("选择的语言").setItems(languages, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(TestActivity.this, "你选择的语言是:" + languages[which], Toast.LENGTH_LONG).show();
}
});
builder2.create().show();
break;
程序运行结果:
为AlertDialog添加可选菜单项:
添加程序片段:
Button button3 = (Button) findViewById(R.id.add_check_list);
button3.setOnClickListener(new ShowAlertDialog());
按钮的事件处理:
case R.id.add_check_list:
final String[] subjects =
{ "房地产开发", "精密仪器", "中药研发", "计算机" };
AlertDialog.Builder builder3 = new AlertDialog.Builder(TestActivity.this);
builder3.setTitle("你的专业").setSingleChoiceItems(subjects, -1, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(TestActivity.this, "你的专业是:" + subjects[which], Toast.LENGTH_SHORT).show();
}
});
builder3.create().show();
break;
程序运行结果: