WEB开发网
开发学院手机开发Android 开发 Android SDK 开发之创建对话框 阅读

Android SDK 开发之创建对话框

 2010-03-23 04:24:00 来源:WEB开发网   
核心提示:An AlertDialog is an extension of the Dialog class. It is capable of constructing most dialog user interfaces and is the suggested dialog type. You should use i

An AlertDialog is an extension of the Dialog class. It is capable of constructing most dialog user interfaces and is the suggested dialog type. You should use it for dialogs that use any of the following features:

一个警告对话框是对话框的一个扩展. 它能够创建大多数对话框用户界面并且是推荐的对话框类新星. 对于需要下列任何特性的对话框,你都应该使用它:

一个标题

一条文字消息

1个-3个按钮

一个可选择的列表(单选框或者复选框)

要创建一个AlertDialog, 使用AlertDialog.Builder子类. 使用AlertDialog.Builder(Context)来得到一个Builder, 然后使用该类的公有方法来定义AlertDialog的属性. 设定好以后, 使用create()方法来获得AlertDialog对象.

下面的主题展示了如何为AlertDialog定义不同的属性, 使用AlertDialog.Builder类. 如果你使用这些示例代码, 你可以在onCreateDialog()中返回最后的Dialog对象来获得图片中对话框的效果.

Adding buttons 增加按钮


要创建一个如图所示的窗口, 使用set...Button()方法:

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setMessage("Are you sure you want to exit?")

.setCancelable(false)

.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

MyActivity.this.finish();

}

})

.setNegativeButton("No", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

dialog.cancel();

}

});

AlertDialog alert = builder.create();

首先,使用setMessage(CharSequence)为对话框增加一条消息。 然后, 开始连续调用方法, 使用setCancelable(boolean)将对话框设为不可取消(不能使用back键来取消)。对每一个按钮,使用set...Button()方法,该方法接受按钮名称和一个DialogInterface.OnClickListener,该监听器定义了当用户选择该按钮时应做的动作。

上一页  1 2 3 4 5 6 7 8  下一页

Tags:Android SDK 开发

编辑录入:coldstar [复制链接] [打 印]
赞助商链接