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

问题描述 投票:0回答:1

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

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

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>
android scope password-encryption
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.