【问题标题】:Get and compare Password from AlertDialog从 AlertDialog 获取和比较密码
【发布时间】:2018-08-14 07:47:28
【问题描述】:

我目前正在尝试使我的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();

【问题讨论】:

    标签: java android passwords android-alertdialog


    【解决方案1】:

    使用 inflator 获取对话框视图,通过该视图,您可以通过调用 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);
    

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

    【讨论】:

    • 谢谢!至少在尝试设置文本时我没有得到 NullPointerException 。我现在遇到的问题是 AlertDialog 出现了,但无论我给它输入什么文本,它什么也没做。
    • 你在尝试什么? setText 还是 getText?
    • 您可以将编辑文本中输入的密码与您共享的首选项密码进行比较,例如 String storedPassword=prefs.getString("password", null); String editTextPassword= pwKey.getText().toString(); if(storedPassword.equals(editTextPassword){ 在这里处理成功}
    • 不知道是不是我实现错了(新代码在 EDIT 中),它仍然没有做任何事情
    • 移动字符串 storedPassword = pref.getString("password",null);字符串 editTextPassword = pwKey.getText().toString();在 setPositiveButton 的 onClick 中,上面 if(storedPassword.equals(editTextPassword))
    猜你喜欢
    • 2021-04-10
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 2013-05-26
    • 2014-06-27
    • 2014-10-10
    • 1970-01-01
    相关资源
    最近更新 更多