密码和确认密码无效

时间:2018-06-01 07:49:20

标签: java android android-studio

我是委员会的新手,我的代码有问题,因为我使用的是TextInputLayout,我希望我的密码和确认密码必须验证,但无论我做什么,它仍然给我解决问题。

这是我的activity_register.xml

<LinearLayout 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"
android:orientation="vertical"
android:padding="16dp"
tools:context=".Register">


<android.support.design.widget.TextInputLayout
    android:id="@+id/text_input_RegPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    app:errorEnabled="true"
    app:passwordToggleEnabled="true">

    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:inputType="textPassword" />

</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:id="@+id/text_input_RegCfmPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/text_input_RegPassword"
    app:errorEnabled="true"
    app:passwordToggleEnabled="true">

    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Confirm Password"
        android:inputType="textPassword" />

</android.support.design.widget.TextInputLayout>

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="101dp"
    android:text="Register"
    android:onClick="Reg"/>

</LinearLayout>

这是我的Register.java,因为我尝试使用等号,但它不会起作用,因为我想输入.getText时只是无效

public class Register extends AppCompatActivity {

private TextInputLayout textInputRegPassword;
private TextInputLayout textInputRegCfmPassword;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    getSupportActionBar().hide();

    setContentView(R.layout.activity_register);

    textInputRegPassword = findViewById(R.id.text_input_RegPassword);
    textInputRegCfmPassword = findViewById(R.id.text_input_RegCfmPassword);

}

private boolean RegisterPassword(){
    String userReg = textInputRegPassword.getEditText().getText().toString().trim();

    if(userReg.isEmpty()){
        textInputRegPassword.setError("Enter Password");
        return false;
    } else {
        textInputRegPassword.setError(null);
        return true;
    }
}

private boolean RegisterCfmPassword(){
    String userReg = textInputRegCfmPassword.getEditText().getText().toString().trim();

    if(userReg.isEmpty()){
        textInputRegCfmPassword.setError("Enter Password");
        return false;
    } else {
        textInputRegCfmPassword.setError(null);
        return true;
    }
}


public void Reg(View v){
    if(!RegisterPassword() | !RegisterCfmPassword() ){
        return;
    }


   }
}

编辑:我想要我的密码和确认密码验证两者是否相同,当我按下按钮时,它就会崩溃

private boolean Verify(){

    if(CfmPassword.getText().toString().equals(Password.getText().toString())){
        return true;
    } else{
        textInputRegCfmPassword.setError("Password Do Not Match");
        return false;
    }
}

public void Reg(View v){
    if(!RegisterPassword() | !RegisterCfmPassword() | !Verify() ){
        return;
    }
}

2 个答案:

答案 0 :(得分:1)

试试此代码

注册类代码

public class Register extends AppCompatActivity {

    private TextInputLayout textInputRegPassword;
    private TextInputLayout textInputRegCfmPassword;
    private TextInputEditText inputRegCfmPassword;
    private TextInputEditText inputRegPassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        getSupportActionBar().hide();

        setContentView(R.layout.activity_register);

        textInputRegPassword = (TextInputLayout)findViewById(R.id.text_input_RegPassword);
        textInputRegCfmPassword =(TextInputLayout) findViewById(R.id.text_input_RegCfmPassword);
        inputRegCfmPassword =(TextInputEditText) findViewById(R.id.input_RegCfmPassword);
        inputRegPassword =(TextInputEditText) findViewById(R.id.input_RegPassword);
    }

    private boolean RegisterPassword(){
        String userReg = inputRegPassword.getText().toString().trim();

        if(userReg.isEmpty()){
            textInputRegPassword.setError("Enter Password");
            return false;
        } else {
            textInputRegPassword.setError(null);
            return true;
        }
    }

    private boolean RegisterCfmPassword(){
        String userReg = inputRegCfmPassword.getText().toString().trim();

        if(userReg.isEmpty()){
            textInputRegCfmPassword.setError("Enter Password");
            return false;
        } else {
            textInputRegCfmPassword.setError(null);
            return true;
        }
    }

    private boolean Verify(){

        if(inputRegPassword.getText().toString().equals(inputRegCfmPassword.getText().toString())){
            return true;
        } else{
            textInputRegCfmPassword.setError("Password Do Not Match");
            return false;
        }
    }

    public void Reg(View v){
        if(!RegisterPassword() || !RegisterCfmPassword() || !Verify() ){
            return;
        }
    }   
}

xml代码是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".Register">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/text_input_RegPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        app:errorEnabled="true"
        app:passwordToggleEnabled="true">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/input_RegPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Password"
            android:inputType="textPassword" />

    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/text_input_RegCfmPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/text_input_RegPassword"
        app:errorEnabled="true"
        app:passwordToggleEnabled="true">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/input_RegCfmPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Confirm Password"
            android:inputType="textPassword" />

    </android.support.design.widget.TextInputLayout>

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="101dp"
        android:onClick="Reg"
        android:text="Register" />

</LinearLayout>

希望这会对你有所帮助!

答案 1 :(得分:0)

TextInputEditText只是一种设计布局。您应该从其子<android.support.design.widget.TextInputLayout android:id="@+id/text_input_RegPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_centerVertical="true" app:errorEnabled="true" app:passwordToggleEnabled="true"> <android.support.design.widget.TextInputEditText android:id="@+id/text_input_edit_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword" /> 获得用户输入。

TextInputEditText

然后在您的活动中定义TextInputEditText password = findViewById(R.id.text_input_edit_password);

password.getText().toString()

最后得到用户输入:

{{1}}