从 AlertDialog 获取并比较密码

Get and compare Password from AlertDialog

我目前正在尝试保护我的 SwitchPreference 密码。当按下该首选项时,应该会出现一个 AlertDialog 要求输入密码。问题是我真的不知道如何从 userinput 获取密码并将其与首选项中的设置值进行比较。

我用我注释掉的方法试了一下,所以你可以看到我试过的。

这是我的PreferencesActivity.java:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
    Preference websitePref = findPreference(getString(R.string.preference_key));
    final SwitchPreference pwPref = (SwitchPreference) findPreference(getString(R.string.sperr_key));


   final  AlertDialog.Builder pw = new AlertDialog.Builder(this);
   final AlertDialog pwD = pw.create();
    pw.setTitle("Authentifikation (Standart:0000)");

    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    String savedWebsite = sharedPrefs.getString(websitePref.getKey(), "");
    websitePref.setSummary(savedWebsite);

    final EditText password = findViewById(R.string.pwKey);


   //     SharedPreferences.Editor editor = getSharedPreferences("password", 0).edit();
 //     editor.putString("password", "test");
 //     editor.commit(); 

    pwPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener(){
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean onPreferenceChange(Preference preference, Object o){
            pw.setView(R.layout.pref_pw);
     //       SharedPreferences pref = getSharedPreferences("password", Context.MODE_PRIVATE);
     //       final String pass = pref.getString("password", null);
     //       password.setText(pass);
            if(pwPref.isChecked()){

                   pw.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
     //                       if(password.equals(pass)) {

                                pwPref.setChecked(false);
     //                       }else{

     //                       }
                        }
                    }).setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    }).show();

            }else{

                pwPref.setChecked(true);

            }
            return false;
        }

    });

}

还有我的pref_pw.xml:

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:id="@string/pwKey"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:inputType="textPassword"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

编辑:

感谢 Roshaan Farrukh,我现在可以在没有错误的情况下获取 AlertDialog,但我无法比较字符串。

我实现了 SharedPreferences

final SharedPreferences.Editor editor = getSharedPreferences("password", Context.MODE_PRIVATE).edit();
    editor.putString("password", "test");
    editor.commit();

然后我将代码更改为:

public boolean onPreferenceChange(Preference preference, Object o){

            pw.setView(R.layout.pref_pw);
            final View view = getLayoutInflater().inflate(R.layout.pref_pw,null,false);
            pw.setView(view);

            final SharedPreferences pref = getSharedPreferences("password", Context.MODE_PRIVATE);
            final EditText pwKey = (EditText) view.findViewById(R.string.pwKey);
            final String storedPassword = pref.getString("password",null);
            final String editTextPassword = pwKey.getText().toString();

            if(pwPref.isChecked()){

                   pw.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            if(storedPassword.equals(editTextPassword)) {
                                pwPref.setChecked(false);

                            }else{

                            }
                        }
                    }).setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    }).show();

使用充气器获取对话框视图,通过该视图,您可以通过调用 view.findViewById(R.id.pwKey) 找到 pwKey Edit 文本。

 View view=getLayoutInflater().inflate(R.layout.pref_pw,null,false);
 EditText pwKey=(EditText) view.findViewById(R.id.pwKey);
 pw.setView(view);

现在您可以将编辑文本中的文本与共享首选项中的文本进行比较。