gpt4 book ai didi

java - 密码和确认密码不起作用

转载 作者:行者123 更新时间:2023-12-02 11:10:48 25 4
gpt4 key购买 nike

我是委员会的新成员,我的代码有问题,因为我正在使用 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;
}
}

最佳答案

试试这个代码

注册类代码

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>

希望这对您有帮助!

关于java - 密码和确认密码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50638555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com