Пароль и подтверждение пароля не работают - PullRequest
0 голосов
/ 01 июня 2018

Я новичок в комитете, и у меня есть проблемы с моим кодом, потому что я использую 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, потому что я пытался использовать equals, но это не сработает, потому что когда я хочу набрать .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 голосов
/ 01 июня 2018

Попробуйте этот код

Зарегистрируйте код класса

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>

Надеюсь, это поможет вам!

0 голосов
/ 01 июня 2018

TextInputLayout это просто макет дизайна.Вы должны получить пользовательский ввод от его дочернего элемента 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()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...