简体   繁体   English

如何从单独类的对话框中返回值

[英]How to return a value from dialog in separate class

I want to create a function that shows a dialog with 2 buttons on screen and return 1 if user pressed OK and 0 if user pressed Cancel. 我想创建一个在屏幕上显示带有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;
}

} }

I am now shure that I am doing right because I do not know how to pass a result from unner class to outer one. 我现在确定自己做对了,因为我不知道如何将结果从较差的类传递给外部的类。 There is a error message 有错误讯息

Cannot refer to a non-final variable rez inside an inner class defined in a different method

So as a result I want to use that function something like this: 因此,我想使用该功能,如下所示:

if (CDlg.ShowConfirm("User confirmation","Delete?",this)==1){
        ...
}

You can't do this like that. 你不能那样做。 ShowConfirm can only show the dialog. ShowConfirm只能显示对话框。 When the user clicks either the OK or Cancel button, only then you can execute what you want: 当用户单击“确定”或“取消”按钮时,才可以执行所需的操作:

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;
            .....
        }
    }
}

The if (CDlg.ShowConfirm("User confirmation","Delete?",this)==1) statement is useless in Android here, since the ShowConfirm will not wait until the user presses a button. if (CDlg.ShowConfirm("User confirmation","Delete?",this)==1)语句在这里在Android中是无用的,因为ShowConfirm不会等到用户按下按钮。

Instead just call ShowConfirm("User confirmation","Delete?"); 而是只调用ShowConfirm("User confirmation","Delete?"); an implement the appropriate code in the onClick s. onClick实现适当的代码。

If you want to code in the spirit of Android, you should actually use startActivityForResult. 如果您希望本着Android的精神进行编码,则应实际使用startActivityForResult。 Look at the linked answer for details how it should work. 请查看链接的答案以获取详细信息。 ( here is the documentation ) 这里是文档

Define a static variable in the class you want, for example I will define in MyAuxiliaryClass.java: 在所需的类中定义一个静态变量,例如,我将在MyAuxiliaryClass.java中定义:

public static USER_DECISION = -1;

Whenever you choose an option, then you do the following: 每当您选择一个选项时,就可以执行以下操作:

if (//Desicion == OK) {
MyAuxiliaryClass.USER_DECISION = 1;
} else (//Decision == NOT OK){
MyAuxiliaryClass.USER_DECISION = 2;
}

Since you are changing this static variable, then you can get the value 1 or 2 in another class. 由于您正在更改此静态变量,因此可以在另一个类中获得值1或2。 Hope it helps. 希望能帮助到你。 Best regards. 最好的祝福。

Make rez an attribute instead of a local variable. rez设为属性而不是局部变量。 As your method is static , the attribute should be too. 由于您的方法是static ,因此属性也应该是static This means moving the definition outside the method. 这意味着将定义移出方法。

public class CDlg { 公共课CDlg {

   static int rez;

   static int ShowConfirm(String caption, String msg, Context context) {
   ...

In the inner classes, you need to refer to the CDlg class 在内部类中,您需要引用CDlg类

   public void onClick(DialogInterface arg0, int arg1) {
        CDlg.rez = 1;
   }

As a side note, it is strange that you use an static method for this. 附带说明一下,为此使用static方法是很奇怪的。 One of the mistakes of the people new to Java/OOP is to abuse static code, that feels more like C was. Java / OOP新手的错误之一是滥用static代码,这更像C Maybe you want to reconsider your code. 也许您想重新考虑您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM