Android从对话框返回值

时间:2018-03-27 18:12:57

标签: java android android-studio view dialog

我正在构建一个Android应用程序。在此代码段中,我尝试调用verifyKey方法,该方法会弹出一个对话框以输入密钥,以便确认密钥是否正确并允许发送消息。

不幸的是,程序不会从调用verifyKey方法的地方返回并运行。关闭对话框后,我找不到程序在代码中的位置,因为Log.d("After verifyInvalid key", "!!!!!!!!!!!!!!!!!");未运行。我已经尝试从onClick中的verifyKey内返回一个布尔值,但它不允许在内部类中使用它。我也得到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);


}

1 个答案:

答案 0 :(得分:0)

将此代码块移至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