【问题标题】:page is not getting intended when button is clicked单击按钮时页面不是预期的
【发布时间】:2021-04-13 14:58:26
【问题描述】:

所以基本上,当我单击注册按钮时,它应该将我定向到下一页,即三节页面。我也为此编写了代码,实际上我已经向页面添加了验证,但是按钮被点击了,但是它没有带我进入下一页。我的意思是下一页没有打开。logcat 部分没有错误。这意味着我的代码正在运行..但是页面没有达到预期,即某处存在一些错误。我尝试解决,但我不明白。

这是我的 new_user_register.java 代码

public class new_user_register extends AppCompatActivity {

    EditText name1;
    EditText email1;
    EditText usname1;
    EditText password1;
    Button register;
    ToastManager toastManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_user_register);


        name1 = (EditText) findViewById(R.id.name1);
        email1 = (EditText) findViewById(R.id.email1);
        usname1 = (EditText) findViewById(R.id.usname1);
        password1 = (EditText) findViewById(R.id.password1);
        register = (Button) findViewById(R.id.register);

        register.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                String NAME = name1.getText().toString().trim();
                String EMAIL = email1.getText().toString().trim();
                String username = usname1.getText().toString().trim();
                String password = password1.getText().toString().trim();


                String emailPattern = "^[a-zA-Z0-9+_.-]{3,32}+@[a-zA-Z0-9.-]{2,32}+$";


                boolean isAtLeastOneFieldNotEmpty = !NAME.isEmpty()
                        || !EMAIL.isEmpty()
                        || !username.isEmpty()
                        || !password.isEmpty();

                toastManager = new ToastManager(new_user_register.this);

                // You have 5 registration fields that users must fill up.
                final int totalRegistrationField = 5;

                // This will increase by 1 when a field is valid
                int validRegistrationFieldCount = 0;

                if (isAtLeastOneFieldNotEmpty) {
                    // NAME VALIDATION
                    if (NAME.isEmpty()) {
                        toastManager.addToast("ENTER NAME", ToastManager.Duration.LENGTH_SHORT);
                    } else if (!((NAME.length() > 3) && (NAME.length() < 15))) {
                        toastManager.addToast("NAME IS TOO SHORT.IT MUST BE BETWEEN 3-15 CHARACTERS.", ToastManager.Duration.LENGTH_SHORT);
                    } else if (!NAME.matches("[a-zA-Z ]+")) {
                        toastManager.addToast("ONLY ALPHABETS ALLOWED", ToastManager.Duration.LENGTH_SHORT);
                    } else {
                        ++validRegistrationFieldCount;
                    }

                    // EMAIL VALIDATION
                    if (EMAIL.isEmpty()) {
                        toastManager.addToast("ENTER EMAIL-ID", ToastManager.Duration.LENGTH_SHORT);
                    } else if (!(EMAIL.matches(emailPattern))) {
                        toastManager.addToast("INVALID EMAIL", ToastManager.Duration.LENGTH_SHORT);
                    } else {
                        ++validRegistrationFieldCount;
                    }


                    // USERNAME VALIDATION
                    if (username.isEmpty()) {
                        toastManager.addToast("ENTER USERNAME", ToastManager.Duration.LENGTH_SHORT);
                    } else if (!((username.length() > 6) && (username.length() < 15))) {
                        toastManager.addToast("USERNAME IS TOO SHORT.IT MUST BE BETWEEN 6-15 CHARACTERS.", ToastManager.Duration.LENGTH_SHORT);
                    } else {
                        ++validRegistrationFieldCount;
                    }

                    // PASSWORD VALIDATION
                    if (password.isEmpty()) {
                        toastManager.addToast("ENTER PASSWORD", ToastManager.Duration.LENGTH_SHORT);
                    } else if (!((password.length() > 6) && (password.length() < 15))) {
                        toastManager.addToast("PASSWORD IS TOO SHORT.IT MUST BE BETWEEN 6-15 CHARACTERS.", ToastManager.Duration.LENGTH_SHORT);
                    } else {
                        ++validRegistrationFieldCount;
                    }
                } else {
                    toastManager.addToast("ALL FIELDS ARE COMPULSORY", ToastManager.Duration.LENGTH_SHORT);
                }

                if (validRegistrationFieldCount != totalRegistrationField) {
                    // Finally show all toast all screen
                    toastManager.show();
                } else {

                    // All registration fields are valid, then go to another screen.
                    Intent p = new Intent(new_user_register.this, three_sections.class);
                    startActivity(p);
                }
            }
        });

    }
}

这是我的 new_user_register.xml 代码

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".loginpage"
        android:background="@drawable/ic_launcher_background">
    
    
        <TextView
            android:layout_width="216dp"
            android:layout_height="68dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_centerInParent="true"
            android:layout_marginEnd="128dp"
            android:layout_marginBottom="561dp"
            android:text="NEW USER REGISTRATION"
            android:textColor="@color/black"
            android:textSize="25dp"
            android:textStyle="bold|italic"
            android:typeface="sans" />
    
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="168dp"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="207dp"
            android:layout_marginBottom="277dp"
            android:drawableLeft="@drawable/username_foreground"
            android:text="USERNAME"
            android:textColor="@color/Purple"
            android:textSize="20dp" />
    
        <TextView
            android:id="@+id/name"
            android:layout_width="115dp"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="256dp"
            android:layout_marginBottom="501dp"
            android:drawableLeft="@drawable/username_foreground"
            android:text="NAME"
            android:textColor="@color/Purple"
            android:textSize="20dp" />
    
        <EditText
            android:id="@+id/name1"
            android:layout_width="291dp"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="80dp"
            android:layout_marginBottom="434dp"
            android:hint="ENTER NAME" />
    
        <TextView
            android:id="@+id/email"
            android:layout_width="144dp"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="229dp"
            android:layout_marginBottom="386dp"
            android:drawableLeft="@drawable/password_foreground"
            android:text="EMAIL ID"
            android:textColor="@color/Purple"
            android:textSize="20dp" />
    
        <EditText
            android:id="@+id/email1"
            android:layout_width="289dp"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="87dp"
            android:layout_marginBottom="326dp"
            android:hint="ENTER EMAIL ID" />
    
        <EditText
            android:id="@+id/usname1"
            android:layout_width="292dp"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="85dp"
            android:layout_marginBottom="220dp"
            android:hint="ENTER USERNAME" />
    
        <TextView
            android:id="@+id/t3"
            android:layout_width="185dp"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="184dp"
            android:layout_marginBottom="170dp"
            android:drawableLeft="@drawable/password_foreground"
            android:text="PASSWORD"
            android:textColor="@color/Purple"
            android:textSize="20dp" />
    
        <EditText
            android:id="@+id/password1"
            android:layout_width="286dp"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="88dp"
            android:layout_marginBottom="113dp"
            android:hint="ENTER PASSWORD" />
    
        <ImageView
            android:layout_width="100dp"
            android:layout_height="110dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="-15dp"
            android:layout_marginBottom="525dp"
            android:src="@drawable/imaagee" />
    
        <ImageView
            android:layout_width="100dp"
            android:layout_height="110dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="-15dp"
            android:layout_marginBottom="408dp"
            android:src="@drawable/image" />
    
        <ImageView
            android:layout_width="100dp"
            android:layout_height="110dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="-15dp"
            android:layout_marginBottom="292dp"
            android:src="@drawable/iimagee" />
    
        <ImageView
            android:layout_width="100dp"
            android:layout_height="110dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="-15dp"
            android:layout_marginBottom="176dp"
            android:src="@drawable/imagee" />
    
        <ImageView
            android:layout_width="100dp"
            android:layout_height="110dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="-15dp"
            android:layout_marginBottom="61dp"
            android:src="@drawable/imageee" />
    
        <Button
            android:id="@+id/register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="210dp"
            android:layout_marginBottom="43dp"
            android:background="@android:color/transparent"
            android:text="REGISTER"
            android:textColor="@color/Purple"
            android:textSize="25dp" />
    
    
    </RelativeLayout>

这是我的三段 xml 代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".three_sections">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="429dp"
        android:alpha="0.4"
        android:background="@color/Purple" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="72dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="-14dp"
        android:layout_marginBottom="472dp"
        android:freezesText="true"
        android:text="     BEGINNERS CALLIGRAPHY"
        android:textSize="25dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="238dp"
        android:alpha="0.4"
        android:background="@color/DeepPink" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="72dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="-27dp"
        android:layout_marginBottom="90dp"
        android:text="     ADVANCED CALLIGRAPHY"
        android:textSize="25dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="47dp"
        android:alpha="0.4"
        android:background="@color/DarkBlue" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="0dp"
        android:layout_marginBottom="315dp"
        android:freezesText="true"
        android:text=" INTERMEDIATE CALLIGRAPHY"
        android:textSize="25dp" />

</RelativeLayout>

【问题讨论】:

  • 您确定程序正在进入最后一个“else”块吗?
  • 是的.....实际上它应该将我重定向到下一页,但该页面不是预期的
  • 提供三段活动代码。
  • 是的.....等等我会添加xml代码或java代码??

标签: android android-studio android-layout android-fragments


【解决方案1】:

您的代码永远不会进入意图块内,因为您的 totalRegistrationFieldvalidRegistrationFieldCount 永远不会匹配。

您已将值 5 分配给 `totalRegistrationField,并且您只需将 validRegistrationFieldCount 增加 4 次。

您只需将 totalRegistrationField 值更改为 4。

final int totalRegistrationField = 4;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-10
    • 2015-07-05
    • 2021-03-02
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    相关资源
    最近更新 更多