gpt4 book ai didi

java - 安卓 : what's the procedure to write functions for alertdialog item?

转载 作者:行者123 更新时间:2023-12-01 15:45:21 24 4
gpt4 key购买 nike

我将在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com