您的位置:首页 > 其它

AlertDialog类的使用

2012-11-25 20:51 489 查看
创建对话框的时候,我们需要通过AlertDialog.Builder(Context context)生成器来构造一个Builder并生成一个AlertDialog。

下面创建含有OK和Cancel两个按钮的dialog对话框。

new AlertDialog.Builder(MyAlertDialog.this)
// MyAlertDialog.this视情况而定,这个一般是指当前显示的Activity对应的xml视窗。
.setTitle("真的要离开?")// 设置对话框的标题
.setMessage("你确定要离开")// 设置对话框的内容
.setPositiveButton("OK",// 设置对话框的确认按钮
new DialogInterface.OnClickListener() {// 设置确认按钮的事件
public void onClick(DialogInterface dialog, int which) {
// do something here..I end this Prograss..
android.os.Process.killProcess(android.os.Process.myPid());
}})
.setNegativeButton("Cancel",// 设置对话框的取消按钮
new DialogInterface.OnClickListener() {// 设置取消按钮的事件
public void onClick(DialogInterface dialog, int which) {
// 如果你什么操作都不做,可以选择不写入任何代码
dialog.cancel();
}}
).show();


一般的格式总结如下:
new AlertDialog.Builder(Context)

        .setTitle()

.setMessage()

.setPositiveButton("OK",

new DialogInterface.OnClickListener(){

pulic void onClick(DialogInterface dialog, int which){

xxx //do something

}})

.setNegativeButton("Cancel",

   new DialogInterface.OnClickListener(){

xxx   //do something

}}).show()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息