[英]How to return a value from dialog in separate class
我想創建一個在屏幕上顯示帶有2個按鈕的對話框的函數,如果用戶按OK,則返回1,如果按Cancel,則返回0。
public class CDlg {
static int ShowConfirm(String caption, String msg, Context context) {
int rez;
AlertDialog.Builder delAllDialog = new AlertDialog.Builder(context);
delAllDialog.setTitle(caption);
TextView dialogTxt_id = new TextView(context);
LayoutParams dialogTxt_idLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
dialogTxt_id.setLayoutParams(dialogTxt_idLayoutParams);
dialogTxt_id.setText(msg);
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(dialogTxt_id);
delAllDialog.setView(layout);
delAllDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
rez = 1;
}
});
delAllDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
rez = 0;
}
});
delAllDialog.show();
return rez;
}
}
我現在確定自己做對了,因為我不知道如何將結果從較差的類傳遞給外部的類。 有錯誤訊息
Cannot refer to a non-final variable rez inside an inner class defined in a different method
因此,我想使用該功能,如下所示:
if (CDlg.ShowConfirm("User confirmation","Delete?",this)==1){
...
}
你不能那樣做。 ShowConfirm
只能顯示對話框。 當用戶單擊“確定”或“取消”按鈕時,才可以執行所需的操作:
public class CDlg {
void ShowConfirm(String caption, String msg) {
AlertDialog.Builder delAllDialog = new AlertDialog.Builder(this);
delAllDialog.setTitle(caption);
TextView dialogTxt_id = new TextView(this);
LayoutParams dialogTxt_idLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
dialogTxt_id.setLayoutParams(dialogTxt_idLayoutParams);
dialogTxt_id.setText(msg);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(dialogTxt_id);
delAllDialog.setView(layout);
delAllDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
handleButtonClick(1);
}
});
delAllDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
handleButtonClick(2);
}
});
delAllDialog.show();
}
void handleButtonClick(int rez) {
switch(rez) {
case 1: ..... break;
case 2: ..... break;
.....
}
}
}
if (CDlg.ShowConfirm("User confirmation","Delete?",this)==1)
語句在這里在Android中是無用的,因為ShowConfirm不會等到用戶按下按鈕。
而是只調用ShowConfirm("User confirmation","Delete?");
在onClick
實現適當的代碼。
如果您希望本着Android的精神進行編碼,則應實際使用startActivityForResult。 請查看鏈接的答案以獲取詳細信息。 ( 這里是文檔 )
在所需的類中定義一個靜態變量,例如,我將在MyAuxiliaryClass.java中定義:
public static USER_DECISION = -1;
每當您選擇一個選項時,就可以執行以下操作:
if (//Desicion == OK) {
MyAuxiliaryClass.USER_DECISION = 1;
} else (//Decision == NOT OK){
MyAuxiliaryClass.USER_DECISION = 2;
}
由於您正在更改此靜態變量,因此可以在另一個類中獲得值1或2。 希望能幫助到你。 最好的祝福。
將rez
設為屬性而不是局部變量。 由於您的方法是static
,因此屬性也應該是static
。 這意味着將定義移出方法。
公共課CDlg {
static int rez;
static int ShowConfirm(String caption, String msg, Context context) {
...
在內部類中,您需要引用CDlg類
public void onClick(DialogInterface arg0, int arg1) {
CDlg.rez = 1;
}
附帶說明一下,為此使用static
方法是很奇怪的。 Java / OOP新手的錯誤之一是濫用static
代碼,這更像C
也許您想重新考慮您的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.