这是我尝试创建的signUp活动,用户输入用户名,emailID和密码。发生的问题是onClick()仅工作一次。假设密码和confirmPassword不匹配,则textlayout显示消息,但如果它再次不匹配则没有任何反应。我帮我错了。
public class SignUp extends AppCompatActivity {
// will send the data into database and return to the login page with a confirm signup message
private final AppCompatActivity activity = SignUp.this;
TextInputEditText usernameText,emailIdText,passwordText,confirmPasswordText;
NestedScrollView nestedScrollView;
TextInputLayout usernameTextLayout, emailIdTextLayout, passwordTextLayout, confirmPasswordTextLayout;
DatabaseManager manager ;
ErrorsHelper helper;
Students student;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_up);
initViews();
initObjects();
}
//views initializer
public void initViews(){
nestedScrollView = (NestedScrollView)findViewById(R.id.signUpNestedScrollView);
// Text Input Layout views
usernameTextLayout = (TextInputLayout) findViewById(R.id.usernameTextLayout);
emailIdTextLayout = (TextInputLayout) findViewById(R.id.emailIdTextLayout);
passwordTextLayout = (TextInputLayout) findViewById(R.id.passwordTextLayout);
confirmPasswordTextLayout = (TextInputLayout) findViewById(R.id.confirmPasswordTextLayout);
// Text Input views
usernameText = (TextInputEditText)findViewById(R.id.usernameText);
emailIdText = (TextInputEditText)findViewById(R.id.emailIdText);
passwordText = (TextInputEditText)findViewById(R.id.passwordText);
confirmPasswordText = (TextInputEditText)findViewById(R.id.confirmPasswordText);
}
// objects initializer
public void initObjects(){
helper = new ErrorsHelper(activity);
manager = new DatabaseManager(activity);
student = new Students();
}
//Register Button
public void onClick(View view){
if(!helper.emptyTextField(usernameText,usernameTextLayout,"Please enter username.")){
return;
}
if(!helper.emptyTextField(emailIdText,emailIdTextLayout,"Please enter email-id.")){
return;
}
if(!helper.emptyTextField(passwordText,passwordTextLayout,"Please enter password.")){
return;
}
if(!helper.emptyTextField(confirmPasswordText,confirmPasswordTextLayout,"Please enter your password to confirm.")){
return;
}
if(!helper.passwordConfirm(passwordText,confirmPasswordText,confirmPasswordTextLayout,"Confirm password doesn't match!")){
return;
}
String username = usernameText.getText().toString();
String emailId = emailIdText.getText().toString();
String confirmPassword = confirmPasswordText.getText().toString();
if (manager.checkUsernameExistence(username)) {
student.set_username(username);
student.set_emailid(emailId);
student.set_password(confirmPassword);
//add student
manager.addStudent(student);
Snackbar.make(nestedScrollView,"Registration Successful.",Snackbar.LENGTH_LONG).show();
Intent intent = new Intent(this,SignInMainPage.class);
startActivity(intent);
}
else{
helper.userExists(usernameTextLayout,"Username already exists.");
}
}
}
ErrorHelper类有助于在一个地方维护所有基于错误的事情。此外,Snackbar没有显示。
public class ErrorsHelper {
Context context;
public ErrorsHelper(Context context) {
this.context = context;
}
public boolean emptyTextField(TextInputEditText editText, TextInputLayout inputLayout, String errorMessage) {
String value = editText.getText().toString().trim();
if (value.isEmpty()) {
inputLayout.setError(errorMessage);
hideKeyboardFrom(editText);
return false;
} else {
inputLayout.setErrorEnabled(false);
}
return true;
}
;
public boolean passwordConfirm(TextInputEditText password, TextInputEditText confirmPassword, TextInputLayout confirmPasswordLayout, String errorMessage) {
String pass = password.getText().toString();
String cPass = confirmPassword.getText().toString();
if (pass != cPass) {
confirmPasswordLayout.setError(errorMessage);
return false;
} else {
confirmPasswordLayout.setErrorEnabled(false);
}
return true;
}
private void hideKeyboardFrom(View view) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
public void userExists( TextInputLayout confirmPasswordLayout, String errorMessage){
confirmPasswordLayout.setError(errorMessage);
}
}
这是我的SignUp页面的XML布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/signUpNestedScrollView"
android:paddingTop="100dp">
<LinearLayout
android:id="@+id/signUpLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:id="@+id/usernameTextLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<android.support.design.widget.TextInputEditText
android:id="@+id/usernameText"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:hint="Username here"
android:fontFamily="casual"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/emailIdTextLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<android.support.design.widget.TextInputEditText
android:id="@+id/emailIdText"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:hint="Email-ID here"
android:fontFamily="casual"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/passwordTextLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<android.support.design.widget.TextInputEditText
android:id="@+id/passwordText"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:hint="Password"
android:inputType="textPassword"
android:fontFamily="casual"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/confirmPasswordTextLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<android.support.design.widget.TextInputEditText
android:id="@+id/confirmPasswordText"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:hint="Confirm Password"
android:inputType="textPassword"
android:fontFamily="casual"/>
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/signupButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:fontFamily="casual"
android:onClick="onClick"
android:text="Sign-Up"
android:textStyle="bold"
android:layout_gravity="center"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
答案 0 :(得分:0)
public void onClick(View view){
// they should be here
String username = usernameText.getText().toString();
String emailId = emailIdText.getText().toString();
String confirmPassword = confirmPasswordText.getText().toString();
if(!username.isEmpty()&&!emailId.isEmpty()&&!confirmPassword.isEmpty())
{
if(!helper.emptyTextField(usernameText,usernameTextLayout,"Please enter username.")){
return;
}
if(!helper.emptyTextField(emailIdText,emailIdTextLayout,"Please enter email-id.")){
return;
}
if(!helper.emptyTextField(passwordText,passwordTextLayout,"Please enter password.")){
return;
}
if(!helper.emptyTextField(confirmPasswordText,confirmPasswordTextLayout,"Please enter your password to confirm.")){
return;
}
if(!helper.passwordConfirm(passwordText,confirmPasswordText,confirmPasswordTextLayout,"Confirm password doesn't match!")){
return;
}
if (manager.checkUsernameExistence(username)) {
student.set_username(username);
student.set_emailid(emailId);
student.set_password(confirmPassword);
//add student
manager.addStudent(student);
//Snackbar.make(findViewById(android.R.id.content),"Registration Successful.",Snackbar.LENGTH_LONG).show();
Snackbar s = Snackbar.make(findViewById(android.R.id.content),"Registration Successful.",Snackbar.LENGTH_LONG);
s.show();
Intent intent = new Intent(this,SignInMainPage.class);
startActivity(intent);
}
else{
helper.userExists(usernameTextLayout,"Username already exists.");
}
}
else
Toast.makeText(getApplicationContext,"Empty",Toast.LENGTH_LONG).show;
}