1、工程目录
2、TestAlertDialogActivity.java
package com.example.testalertdialog;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class TestAlertDialogActivity extends Activity {
private TextView myTextView;
private Button myButton;
private TextView myTextView2;
private Button myButton2;
private Button myButton3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_alert_dialog);
myTextView = (TextView) findViewById(R.id.layoutTextView01);
myButton = (Button) findViewById(R.id.layoutButton01);
myTextView2 = (TextView) findViewById(R.id.layoutTextView02);
myButton2 = (Button) findViewById(R.id.layoutButton02);
// 为什么builder必须在这儿定义为final类型呢???
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
final AlertDialog.Builder builder3 = new AlertDialog.Builder(this);
final String[] items = { "奥尔良鸡腿堡", "麻辣鸡腿堡", "咖啡" };
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
builder.setMessage("真的要删除该信息吗?")
.setPositiveButton("是",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0,
int arg1) {
// TODO Auto-generated method stub
myTextView.setText("删除成功!");
}
})
.setNegativeButton("否",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0,
int arg1) {
// TODO Auto-generated method stub
myTextView.setText("取消删除!");
}
}).create().show();
}
});
// 选择后即退出对话框
myButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
builder2.setTitle("请点餐")
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
myTextView2.setText(items[arg1]);
}
}).create().show();
}
});
// 以单选按钮样式显示,不退出,需按“Back”
myButton3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
builder3.setTitle("请点餐")
.setSingleChoiceItems(items, -1,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0,
int arg1) {
// TODO Auto-generated method stub
myTextView2.setText(items[arg1]);
}
}).create().show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test_alert_dialog, menu);
return true;
}
}
3、布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/layoutTextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/testAlertDialog" />
<Button
android:id="@+id/layoutButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/del" />
<TextView
android:id="@+id/layoutTextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/items" />
<Button
android:id="@+id/layoutButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/diancan" />
<Button
android:id="@+id/layoutButton03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/diancan2" />
</LinearLayout>
4、资源文件String.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">testAlertDialog</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="testAlertDialog">TestAlertDialog</string>
<string name="del">删除文本框信息</string>
<string name="items">AlertDialog多条目测试</string>
<string name="diancan">点餐系统</string>
<string name="diancan2">点餐系统(点选按钮)</string>
</resources>
遇到的问题:AlertDialog创建Builder的时候只能创建成final,不能直接使用new AlertDialog。为什么?
final的用法:匿名内部类的方法中使用的变量必须定义为final类型。就是为了防止在调用该变量时,该变量的引用被修改,导致出现无法预料的问题。