【问题标题】:Android return value from dialogAndroid从对话框返回值
【发布时间】:2018-03-27 18:12:56
【问题描述】:

我正在构建一个安卓应用程序。在这个 sn-p 中,我尝试调用verifyKey 方法,该方法会弹出一个对话框输入密钥,以便确认它是否正确并允许发送消息。

不幸的是,程序没有返回并从调用verifyKey 方法的位置运行。关闭对话框后,我找不到程序在代码中的位置,因为Log.d("After verifyInvalid key", "!!!!!!!!!!!!!!!!!"); 没有运行。我尝试从verifyKeyonClick 中返回一个布尔值,但它不允许从内部类中返回。我也收到了E/ViewRootImpl: sendUserActionEvent() mView == null

是否有返回此对话框的简单方法,或者我如何确保代码在从调用对话框时的位置运行时返回。

this.testContact = new Contacts("testCon", "08xxxxxxx", "testkey", true);
    // assign on click listener to button
    send_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //to send a message first an encryption key must be established
            if (testContact.getKeySet()) {
                verifyKey();
                if (keyChecker(verifyEnteredKey)) {
                    Log.d("Valid key", "!!!!!!!!!!!!!!!!!");
                    sendSms(testContact);
                } else {
                    Toast.makeText(getBaseContext(), "Invalid  Key",
                            Toast.LENGTH_LONG).show();
                }
                Log.d("After verifyInvalid key", "!!!!!!!!!!!!!!!!!");

                Log.d("OUTSDIDE KEY CEHCK", "outside");
            }
        }//on click
    });
 public boolean keyChecker(String entKey) {
    if (entKey.equals(testContact.getKey())) {
        Log.d("Valid key", "key confirmed");
        return true;
    }
    Log.d("inValid key", "key un-confirmed");
    return false;
}

public void verifyKey() {
    verifyEnteredKey = "";
    Builder builder = new Builder(SendMessage.this);
    builder.setTitle("Cloaked Key");
    // I'm using fragment here so I'm using getView() to provide ViewGroup
    // but you can provide here any other instance of ViewGroup from your Fragment / Activity
    View viewInflated = LayoutInflater.from(SendMessage.this).inflate(R.layout.key_entry_dialog, (ViewGroup) findViewById(android.R.id.content), false);
    // Set up the input
    final EditText input = viewInflated.findViewById(R.id.input);
    // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
    builder.setView(viewInflated);

    // Set up the buttons
    builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            verifyEnteredKey = input.getText().toString();
            Log.d("ENTERED KEYY in dialog", verifyEnteredKey);
            // if(verifyEnteredKey.equals(testContact.getKey())){
            //    return true;
            // }
            // keyChecker(verifyEnteredKey);
            Log.d("Before dialog dismiss", verifyEnteredKey);
            dialog.dismiss();
            Log.d("afteer dialog dismiss", verifyEnteredKey);

        }

    });
    builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    Log.d("Before builder show", verifyEnteredKey);
    builder.show();
    Log.d("After builder show", verifyEnteredKey);


}

【问题讨论】:

  • 您不能从匿名类内部的方法返回。在这种情况下,您应该从内部类方法中进一步了解流程。您可以从内部类内部调用方法,也可以使用回调。见How to define callbacks
  • 感谢您从对话框中调用keyChecker(verifyEnteredKey);,然后从 keyChecker 方法中调用`sendSms(testContact);`。我唯一的问题是我想重新使用此方法(输入要发送的密钥和输入要查看消息的密钥),但这种方式不允许我这样做
  • 为了代码的可重用性,您应该遵循设计模式。或者,您可以简单地从创建具有通用对象的 Utility 类开始。深入这个你会得到这个概念。祝你好运。
  • 完美,非常有帮助。

标签: java android android-studio view dialog


【解决方案1】:

将此代码块移至onDismissListener https://developer.android.com/reference/android/content/DialogInterface.OnDismissListener.html

if (keyChecker(verifyEnteredKey)) {
                Log.d("Valid key", "!!!!!!!!!!!!!!!!!");
                sendSms(testContact);
            } else {
                Toast.makeText(getBaseContext(), "Invalid  Key",
                        Toast.LENGTH_LONG).show();
    }

本质上,您是在尝试在回调中分配值之前读取verifyEnteredKey

【讨论】:

    猜你喜欢
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多