如何使用 'show password' 选项模拟 Android 输入密码对话框

How to simulate the Android enter password dialog box with 'show password' option

我已经能够在 xml 中为警告对话框创建布局,其中包含 header 文本、编辑文本(用于密码)和 'show password' 的复选框。当对话框关闭时,我能够检索密码。我无法做的是响应点击复选框。

我已经尝试过多次使用“Builder.setMultiChoiceItems”的尝试,但使用该方法除了我在布局中的复选框外还添加了它的 OWN 复选框。此外,将其粘贴在标题下而忽略我的布局。我不想在那里!处理程序响应对恶意复选框的点击,但不响应我的复选框。

那么如何使用 Builder.setMultiChoiceItems 方法并在布局中使用复选框?或者,如果这不可能,我如何控制 Builder.setMultiChoiceItems 放置复选框的位置?

是否有另一种方法可以在不关闭对话框的情况下响应对我的复选框的点击? (我可以在退出对话框时读取复选框的状态,但这不是我想要的!)

我的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:padding="24dp">

    <android.support.v7.widget.AppCompatTextView
        style="@style/Base.TextAppearance.AppCompat.Subhead"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="24dp"
        android:text="@string/enter_password" />

    <EditText
        android:id="@+id/edit_network_password"
        style="@style/Widget.AppCompat.EditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp"
        android:inputType="textPassword"
        android:hint="@string/network_password"
        android:textColorHint="#ffff25e6"/>

    <android.support.v7.widget.AppCompatCheckBox
        android:id="@+id/do_not_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/show_password" />

</LinearLayout>

我的代码

    LayoutInflater inflater = getLayoutInflater();
    final View dialogContent = inflater.inflate(R.layout.dialog_password_notification, null);
    final String[] items = {getString(R.string.show_password)};

    android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context);
    builder.setTitle(R.string.password_dialog_title);
    final EditText edit = dialogContent.findViewById(R.id.edit_network_password);
    builder.setView(dialogContent);
    builder.setMultiChoiceItems(items, null,
            new DialogInterface.OnMultiChoiceClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int selectedItemId,
                                    boolean isSelected)
                {
                    Log.i(TAG, "Got something!");
                    if (selectedItemId == R.id.do_not_show)
                    {
                        if(isSelected)
                        {
                            Log.i(TAG, "Check box Show password");
                            // Show the password
                        }
                        else
                        {
                            // Hide the password
                            Log.i(TAG, "Check box Hide password");
                        }

                    }
                }
            });
    builder.setPositiveButton("Done!", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int id)
            {
                LniHubAssistantActivity.selectedWifi.setPasskey(edit.getText().toString());
                Prefs.saveToSharedPreference(context, Prefs.WIFI_KEY, LniHubAssistantActivity.selectedWifi.getPasskey());
                //Your logic when OK button is clicked
                finish();
            }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int id)
            {
            }
        });
    android.support.v7.app.AlertDialog dialog = builder.create();
    dialog.show();

您可以将侦听器附加到复选框,如下所示:

    AppCompatCheckBox showPassword = dialogContent.findViewById(R.id.do_not_show);
    showPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (!isChecked) {
                // hide password
                edit.setTransformationMethod(PasswordTransformationMethod.getInstance());
                edit.setSelection(edit.getText().length());
            } else {
                // show password
                edit.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                edit.setSelection(edit.getText().length());
            }
        }
    });