gpt4 book ai didi

Android:在 toast 警报对话框中切换大小写

转载 作者:行者123 更新时间:2023-11-29 20:45:57 31 4
gpt4 key购买 nike

Android:在 Toast 警报对话框中切换大小写。

我想在返回时获取字符串。这是我的代码:

public String getValue(final int x) {

final String[] c = new String[1];


AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setItems(new CharSequence[]
{"1", "2", "3"},
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
switch (which) {
case 0:
Toast.makeText(context, "clicked 1", Toast.LENGTH_LONG).show();
c[0] = "1";
break;
case 1:
Toast.makeText(context, "clicked 2", Toast.LENGTH_LONG).show();
c[0] = "2";

break;
case 2:
Toast.makeText(context, "clicked 3", Toast.LENGTH_LONG).show();
c[0] = "3";
break;
}
}
});
builder.create().show();

return c[0];
}

但是它不返回任何东西!

有人知道吗?

最佳答案

onClick 有返回拼写错误 void,因此您可以从那里返回一个值,而不会导致编译时错误。您可以做的是创建一个处理输入的方法,并从 onClick 调用它。例如

public void myMethod(String input) {
// do something with input
}

并调用它

public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
switch (which) {
// cases
}
myMethod(c[0]);
// the other code

关于Android:在 toast 警报对话框中切换大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30355777/

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