【问题标题】:How to simulate the Android enter password dialog box with 'show password' option如何使用“显示密码”选项模拟 Android 输入密码对话框
【发布时间】:2018-07-13 22:49:39
【问题描述】:

我已经能够在 xml 中为带有标题文本、编辑文本(用于密码)和“显示密码”复选框的警报对话框创建布局。对话框关闭时,我可以检索密码。我一直无法做的是响应点击复选框。

我已经尝试过无数次使用“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();

【问题讨论】:

    标签: android checkbox android-alertdialog


    【解决方案1】:

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

        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());
                }
            }
        });
    

    【讨论】:

    • 谢谢。您应该在答案中补充说,使用“setMultichoiceItems”不是可行的方法!
    猜你喜欢
    • 2022-01-25
    • 2011-04-29
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 2012-03-22
    • 2017-10-02
    相关资源
    最近更新 更多