[英]Android: Switch case in toast alert Dialog
Android:在Toast Alert對話框中切換大小寫。
我想得到返回的字符串。 這是我的代碼:
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
具有返回拼寫錯誤,因此您可以從那里返回一個值,而不會引起編譯時錯誤,因此沒有任何變化。 您可以做的是創建一個處理輸入的方法,然后從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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.