当前位置: 首页 > 知识库问答 >
问题:

如何设置onClick进行输入验证?

许茂才
2023-03-14

我面临的问题是用户输入没有经过验证。当我单击regSignUp2Btn按钮时,它将直接跳转到下一个屏幕。当用户没有提供验证信息时,它应该在TextInputLayout下提供一个错误消息。我的代码有错误吗?下面是我的代码。Java:

    public class signUpScreen extends AppCompatActivity {

    //Variables
    TextInputLayout regEmail, regName, regPassword, regConfirmPassword;
    TextInputEditText emailEt, nameEt, passwordEt, confirmPasswordEt;
    Button regSignUp2Btn, regToSignInBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_sign_up_screen);

        regEmail = findViewById(R.id.emailAdd);
        regName = findViewById(R.id.name);
        regPassword = findViewById(R.id.password);
        regConfirmPassword = findViewById(R.id.confirmPassword);
        regSignUp2Btn = findViewById(R.id.goToSignUp2Btn);
        regToSignInBtn = findViewById(R.id.regToSignInBtn);
        emailEt = findViewById(R.id.emailAddET);
        nameEt = findViewById(R.id.nameET);
        passwordEt = findViewById(R.id.passwordET);
        confirmPasswordEt = findViewById(R.id.confirmPasswordET);

        regSignUp2Btn.setOnClickListener(v -> {
            //Get all the values
            String name = regName.getEditText().getText().toString().trim();
            String email = regEmail.getEditText().getText().toString().trim();
            String password = regPassword.getEditText().getText().toString();

            Intent intent = new Intent(signUpScreen.this, signUpScreen2.class);
            intent.putExtra("name", name);
            intent.putExtra("email", email);
            intent.putExtra("password", password);
            startActivity(intent);

        }); 

        regToSignInBtn.setOnClickListener(view -> {
            Intent intent = new Intent(signUpScreen.this, signInScreen.class);
            startActivity(intent);
        });

    } 

    private boolean validateName() {
        String val = regName.getEditText().getText().toString().trim();

        if (val.isEmpty()) {
            regName.setError("Field cannot be empty");
            return false;
        } else if (val.length() > 30) {
            regName.setError("Username too long");
            return false;
        } else {
            regName.setError(null); //remove error
            regName.setErrorEnabled(false); //remove space
            return true;
        }
    }

    private boolean validateEmail() {
        String val = regEmail.getEditText().getText().toString();
        String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+.+[a-z]+";
        String noWhiteSpace = "\\A\\w{1,30}\\z";

        if (val.isEmpty()) {
            regEmail.setError("Field cannot be empty");
            return false;
        } else if (!val.matches(emailPattern)) {
            regEmail.setError("Invalid email address");
            return false;
        } else if (!val.matches(noWhiteSpace)) {
            regEmail.setError("White spaces are not allowed");
            return false;
        } else {
            regEmail.setError(null);
            regEmail.setErrorEnabled(false);
            return true;
        }
    }

    private boolean validatePassword() {
        String val = regPassword.getEditText().getText().toString();
        String passwordVal = "^" +
                "(?=.*[0-9])" +         //at least 1 digit
                "(?=.*[a-z])" +         //at least 1 lower case letter
                "(?=.*[A-Z])" +         //at least 1 upper case letter
                "(?=.*[a-zA-Z])" +      //any letter
                "(?=.*[@#$%^&+=])" +    //at least 1 special character
                "(?=\\S+$)" +           //no white spaces
                ".{4,}" +               //at least 4 characters
                "$";

        if (val.isEmpty()) {
            regPassword.setError("Field cannot be empty");
            return false;
        } else if (!val.matches(passwordVal)) {
            regPassword.setError("Password is too weak. Should have 1 symbol, 1 digit, 1 lower case, 1 upper case and at least 4 characters");
            return false;
        } else {
            regPassword.setError(null);
            regPassword.setErrorEnabled(false);
            return true;
        }
    }

    private boolean validateConfirmPassword() {
        String val = regPassword.getEditText().getText().toString();
        String val1 = regConfirmPassword.getEditText().getText().toString();

        if (val.isEmpty()) {
            regConfirmPassword.setError("Field cannot be empty");
            return false;
        } else if (!val.equals(val1)) {
            regPassword.setError(null);
            regConfirmPassword.setError(null);
            regPassword.setErrorEnabled(false);
            regConfirmPassword.setErrorEnabled(false);
            regConfirmPassword.setError("Password is not same");
            return false;
        } else {
            return true;
        }
    }

    public void call2ndSignUpScreen(View view) {
        if (!validateEmail() | !validateName() | !validatePassword() | !validateConfirmPassword()) {
            return;
        }
    }

XML:

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    tools:context=".signUpScreen"
    android:background="@color/white"
    android:padding="30dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/signup_back_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:src="@drawable/ic_baseline_arrow_back_ios_24"
            app:tint="@color/black"
            android:contentDescription="@string/backbtn" />

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="227dp"
            android:layout_height="117dp"
            android:layout_gravity="center"
            android:contentDescription="@string/todo"
            app:srcCompat="@drawable/crop" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/create_your_account"
            android:textSize="12sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:orientation="vertical">

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/emailAdd"
                style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/email_address"
                app:boxBackgroundColor="@color/white"
                app:boxStrokeColor="@color/black"
                app:boxStrokeWidthFocused="2dp"
                app:endIconMode="clear_text"
                app:endIconTint="@color/black"
                app:startIconDrawable="@drawable/ic_baseline_mail_outline_24"
                app:startIconTint="@color/black">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/emailAddET"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textEmailAddress" />

            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/name"
                style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:hint="@string/full_name_as_per_ic"
                app:counterEnabled="true"
                app:counterMaxLength="30"
                app:boxBackgroundColor="@color/white"
                app:boxStrokeColor="@color/black"
                app:boxStrokeWidthFocused="2dp"
                app:endIconMode="clear_text"
                app:endIconTint="@color/black"
                app:startIconDrawable="@drawable/ic_baseline_person_outline_24"
                app:startIconTint="@color/black">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/nameET"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="text" />

            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/password"
                style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/password"
                app:passwordToggleEnabled="true"
                app:boxBackgroundColor="@color/white"
                app:boxStrokeColor="@color/black"
                app:boxStrokeWidthFocused="2dp"
                app:endIconMode="clear_text"
                app:endIconTint="@color/black"
                app:startIconDrawable="@drawable/ic_outline_lock_24"
                app:startIconTint="@color/black">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/passwordET"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textPassword" />

            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/confirmPassword"
                style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:hint="@string/confirm_password"
                app:passwordToggleEnabled="true"
                app:boxBackgroundColor="@color/white"
                app:boxStrokeColor="@color/black"
                app:boxStrokeWidthFocused="2dp"
                app:endIconMode="clear_text"
                app:endIconTint="@color/black"
                app:startIconDrawable="@drawable/ic_outline_lock_24"
                app:startIconTint="@color/black">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/confirmPasswordET"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textPassword" />

            </com.google.android.material.textfield.TextInputLayout>

        </LinearLayout>

        <Button
            android:id="@+id/goToSignUp2Btn"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="145dp"
            android:layout_height="50dp"
            android:layout_gravity="center_horizontal|center"
            android:background="@color/actionBarColor"
            android:contentDescription="@string/next"
            android:gravity="center"
            android:text="@string/next"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_marginTop="30dp"
            android:onClick="call2ndSignUpScreen"/>

        <Button
            android:id="@+id/regToSignInBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00000000"
            android:gravity="center"
            android:text="@string/already_have_an_account_sign_in_now"
            android:textColor="@color/black"
            android:textSize="12sp" />

    </LinearLayout>

</ScrollView>

共有1个答案

蔚桐
2023-03-14

在您的代码中,您正在使用OnClickListener

regSignUp2Btn.setOnClickListener{ 
       /* ....*/
       Intent intent = new Intent(signUpScreen.this, signUpScreen2.class);
       //...
       startActivity(intent);
}

该侦听器不提供验证,并覆盖布局中使用的android:onclick=“call2ndSignupScreen”

更改为:

regSignUp2Btn.setOnClickListener{ 

      if (!validateEmail() | !validateName() | !validatePassword() | !validateConfirmPassword()) {
            //setError
            return;
      }
      //....your code
 }
 类似资料:
  • 我是一个新的pact学习者,我想知道当提供程序验证时我应该输入什么 对于提供程序验证,我应该将提供的目标填充为本地主机,或者代替本地主机,我也可以输入实际环境的主机?哪种场景最适合合同测试?

  • 我有一个组件可能会或可能不会收到onClick属性。我想附加这个非必需的道具作为点击处理程序。 如果我不知道是否会给处理程序,那么最安全的方法是什么?

  • 您可以对以下企业内容进行设置 基本信息 企业认证 用车地点 增值服务的开关配置 一、基本信息 企业名称:如您已完成资质认证,只能通过资质认证进行修改 中文简称:必填,中文简称将显示在滴滴企业版APP、滴滴出行APP中 联系人:选填,方便滴滴工作人员为您的企业提供更好服务 联系电话:选填,方便滴滴工作人员为您的企业提供更好服务 英文简称:选填,当滴滴出行APP为英文版时,有保险时显示 所属行业:选填

  • 问题内容: 我正在为即将推出的Chrome网上商店创建一个网络应用。有没有办法模拟F11被按下?还是简单地使当前窗口全屏显示的命令? 问题答案:

  • 我试图找到一个完整的模式列表,用于通过HTML5表单验证各种类型的输入,特别是、、等等,但我找不到任何模式。目前,这些输入验证的内置版本远非完美(甚至不检查输入的内容是否是电话号码)。所以我想知道,我可以使用哪些模式来验证用户在输入中输入了正确的格式? 以下是一些默认验证允许不允许的输入的例子: 这个字段允许在@后面有不正确域的电子邮件,它允许地址以破折号或句号开始或结束,这也是不允许的。因此,是

  • 大家好,我有代码主和tictactoe类 这里是main类的示例。在这段代码中,我输入了类似字符串号的内容 这是为我的tictactoe程序班准备的。如果我运行这个代码,我只有3x3tictactoe程序,所以我想用我的输入修改其中一个代码,这样我的tictactoe将输入x输入 我的问题是我想改变这个领域 成为 公共静态最终int行=输入;//行按COLS单元格public static fin