我将在 AlertDialog 的项目中编写一些函数: - 代码是: -
final CharSequence[] items = {"Now", "Later", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("When you need to Take BackUp?");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item)
{
// Toast.makeText(getApplicationContext(),items[item],Toast.LENGTH_SHORT).show();
items[0].notify();
items[1].charAt(2);
}
});
builder.show();
该项目将启用某些功能,我如何声明和定义它。
最佳答案
所选的item
将被传递到onClick
方法中,即该方法签名中的int item
。
public void onClick(DialogInterface dialog, int item) {
switch(item) {
case 0: // the first item in the CharSequence[]
callUserClickedNow();
break;
case 1: // the second item in the CharSequence[]
callUserClickedLater();
break;
case 2: // the thirditem in the CharSequence[]
callUserClickedCancel();
break;
}
dialog.dismiss();
}
然后在创建 AlertDialog
的方法之外:
private void callUserClickedNow() {
// do stuff
}
等等...
关于java - 安卓 : what's the procedure to write functions for alertdialog item?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7148693/