如何检查输入的密码是否正确?

时间:2019-04-30 17:33:13

标签: android scope password-encryption

我试图以对话框布局返回它,但是调试给了我变量的空值 我正在尝试检查密码是否正确,但这给了我一个空对象错误

这里是我从数据库返回密码的功能,它知道此功能在主活动中,而密码确认不是此活动的上下文

public void showDialog(final Intent i){
    LayoutInflater factory = LayoutInflater.from(this);
    final View deleteDialogView = factory.inflate(R.layout.password_confirm, null);
    final AlertDialog deleteDialog = new AlertDialog.Builder(this).create();
    deleteDialog.setView(deleteDialogView);
    deleteDialogView.findViewById(R.id.confirmPasswordCheck).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            db.collection("Departments").document(DeparmentName).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    TextInputLayout textInputLayout = findViewById(R.id.text_input_password_confirm);
                    EditText editText = findViewById(R.id.edit_password_check);
                    passwordReturn = task.getResult().getString("Password");
                    if(editText.getText().equals(passwordReturn)){
                        startActivity(i);
                    }else{
                        textInputLayout.setError("Password is wrong");
                    }
                }
            });
        }
    });
    deleteDialogView.findViewById(R.id.cancelPasswordCheck).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            deleteDialog.dismiss();
        }
    });

    deleteDialog.show();
}

这里是我调用函数的地方以及发送给它的东西

        Intent i = new Intent(MainActivity.this, AddPatientActivity.class);
        i.putExtra("SuperSession", SuperKey);
        i.putExtra("DepartmentName", DeparmentName);
        showDialog(i);

这是布局

    <?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_gravity="center"
    tools:context=".MainActivity"
    android:background="@color/blueState">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/txt_password_confirm"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:textSize="24sp" />
    <android.support.design.widget.TextInputLayout
        android:id="@+id/text_input_password_confirm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:theme="@style/MyEditText"
        app:errorEnabled="true"
        app:errorTextAppearance="@style/MyErrorText"
        app:passwordToggleEnabled="true">

        <EditText
            android:id="@+id/edit_password_check"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/century_gothic"
            android:hint="@string/password"
            android:imeOptions="flagNoExtractUi"
            android:inputType="textPassword"
            android:theme="@style/MyEditText" />
    </android.support.design.widget.TextInputLayout>
        <Button
            android:id="@+id/confirmPasswordCheck"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/rounded_green_btn"
            android:text="Confirm"/>
        <Button

            android:id="@+id/cancelPasswordCheck"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/rounded_red_btn"
            android:text="@string/cancelBtn"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

您应在setOnClickListener()之前声明视图绑定。

TextInputLayout textInputLayout = findViewById(R.id.text_input_password_confirm);
EditText editText = findViewById(R.id.edit_password_check);

然后,您应该使用editText.getText().toString().equals(passwordReturn)而不是editText.getText().equals(passwordReturn)。因为editText.getText()返回Editable