首页 > 解决方案 > 当我点击注册按钮时,它应该将我重定向到下一页,但这并没有发生

问题描述

所以,基本上当我点击注册按钮时,它应该将我重定向到下一页。我尝试了各种方法,但没有奏效。即使没有错误,当我点击按钮时,它仍然被点击但没有被定向到下一页.我已经粘贴了下面的所有代码。任何帮助将不胜感激。

这是我的 new_user_login.xml 文件。

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
    tools:context=".NewUserActivity"
    android:background="@drawable/ic_launcher_background"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/textView4"
        android:layout_width="276dp"
        android:layout_height="38dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="106dp"
        android:layout_marginBottom="581dp"
        android:text="   REGISTRATION "
        android:textColor="@color/WhiteSmoke"
        android:textSize="30dp" />

    <EditText
        android:id="@+id/name"
        android:layout_width="298dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="36dp"
        android:layout_marginBottom="487dp" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="218dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="120dp"
        android:layout_marginBottom="530dp"
        android:drawableLeft="@drawable/ic_action_name"
        android:text="NAME"
        android:textColor="@color/WhiteSmoke"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="195dp"
        android:layout_height="30dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="138dp"
        android:layout_marginBottom="440dp"
        android:drawableLeft="@drawable/ic_action_mail"
        android:text="EMAIL ID"
        android:textColor="@color/WhiteSmoke"
        android:textSize="20dp" />

    <EditText
        android:id="@+id/email"
        android:layout_width="298dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="36dp"
        android:layout_marginBottom="388dp" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="147dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="186dp"
        android:layout_marginBottom="351dp"
        android:drawableLeft="@drawable/ic_action_phone"
        android:text="PHONE NO."
        android:inputType="number"
        android:textColor="@color/WhiteSmoke"
        android:textSize="20dp" />

    <EditText
        android:id="@+id/phone"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="36dp"
        android:layout_marginBottom="306dp" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="195dp"
        android:layout_height="30dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="135dp"
        android:layout_marginBottom="254dp"
        android:drawableLeft="@drawable/ic_action_username"
        android:text="USERNAME"
        android:textColor="@color/WhiteSmoke"
        android:textSize="20dp" />

    <EditText
        android:id="@+id/usname"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="36dp"
        android:layout_marginBottom="205dp" />


    <TextView
        android:id="@+id/textView9"
        android:layout_width="195dp"
        android:layout_height="30dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="131dp"
        android:layout_marginBottom="165dp"
        android:drawableLeft="@drawable/ic_action_password"
        android:text="PASSWORD"
        android:textColor="@color/WhiteSmoke"
        android:textSize="20dp" />

    <EditText
        android:id="@+id/passsword"
        android:layout_width="295dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="36dp"
        android:layout_marginBottom="113dp" />


    <Button
        android:id="@+id/register"
        android:layout_width="324dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="13dp"
        android:layout_marginBottom="23dp"
        android:background="@color/DeepPink"
        android:fontFamily="sans-serif"
        android:onClick="registration"
        android:text="REGISTER"
        android:textColor="@color/Black"
        android:textSize="24dp" />


</RelativeLayout>

这是 NewUserACtivity.java 文件

    package com.example.moviebookingapp2;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    public class NewUserActivity extends AppCompatActivity {
    
        EditText name;
        EditText email;
        EditText phone;
        EditText usname;
        EditText passsword;
        Button register;
        ToastManager toastManager;
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.new_user_login);
    
    
    
            name = (EditText) findViewById(R.id.name);
            email = (EditText) findViewById(R.id.email);
            phone = (EditText) findViewById(R.id.phone);
            usname = (EditText) findViewById(R.id.usname);
            passsword = (EditText) findViewById(R.id.passsword);
            register = (Button) findViewById(R.id.register);
    
    
            register.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    String NAME = name.getText().toString().trim();
                    String EMAIL = email.getText().toString().trim();
                    String PHONENO = phone.getText().toString().trim();
                    String username = usname.getText().toString().trim();
                    String password = passsword.getText().toString().trim();
                    String emailPattern = "^[a-zA-Z0-9+_.-]{3,32}+@[a-zA-Z0-9.-]{2,32}+$";
                    String phonePattern = "(0/91)?[7-9][0-9]{9}";
    
    
                    boolean isAtLeastOneEditTextNotEmpty = !NAME.isEmpty()
                            || !EMAIL.isEmpty()
                            || !PHONENO.isEmpty()
                            || !username.isEmpty()
                            || !password.isEmpty();
    
    
                    ToastManager toastManager = new ToastManager(NewUserActivity.this);
    
                    if (isAtLeastOneEditTextNotEmpty) {
                        // 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);
                        }
    
                        //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);
                        }
    
                       //PHONE NUMBER VALIDATION
                        if (PHONENO.isEmpty()) {
                            toastManager.addToast("ENTER PHONE NO.", ToastManager.Duration.LENGTH_SHORT);
                        } else if (!(PHONENO.length() == 10)) {
                            toastManager.addToast("INVALID PHONE NO.", ToastManager.Duration.LENGTH_SHORT);
                        } else if (!(PHONENO.matches(phonePattern))) {
                            toastManager.addToast("INVALID PHONE NO.", ToastManager.Duration.LENGTH_SHORT);
                        }
    
                        //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);
                        }
    
                        //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 {
                        toastManager.addToast("ALL FIELDS ARE COMPULSORY", ToastManager.Duration.LENGTH_SHORT);
                    }
    
                   // Finally show all toast all screen
                    toastManager.show();
                }
            });
        }
    
        @Override
        protected void onStop() {
            super.onStop();
    
        }
    
        public void registration(View view) {
            register.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent p = new Intent(NewUserActivity.this,MainActivity2.class);
                    startActivity(p);
                }
            });
        }
    }

this is the page that shoild open when i will click that register button.
this is my MAinActivity2.java code

package com.example.moviebookingapp2;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity2 extends AppCompatActivity implements ClickedListener {
    RecyclerView recycleview1;
    List<String> titles;
    List<Integer>images;
    GridAdapter adapter;
    Button register;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.intropage);


        recycleview1 = findViewById(R.id.recycleview1);

        titles = new ArrayList<>();
        images = new ArrayList<>();

        titles.add("LATEST MOVIES");
        titles.add("UPCOMING MOVIES");
        titles.add("TRENDING MOVIES");
        titles.add("NOW PLAYING");
        titles.add("RELEASED MOVIES");
        titles.add("HINDI MOVIES");
        titles.add("MARATHI MOVIES");
        titles.add("ENGLISH MOVIES");
        titles.add("TELUGU MOVIES");
        titles.add("TAMIL MOVIES");


        images.add(R.drawable.latestmovie);
        images.add(R.drawable.upcomingmovies);
        images.add(R.drawable.trendingmovie);
        images.add(R.drawable.nowplaying);
        images.add(R.drawable.releasedmovie);
        images.add(R.drawable.hindimovies);
        images.add(R.drawable.marathimovies);
        images.add(R.drawable.englishmovies);
        images.add(R.drawable.telugumovies);
        images.add(R.drawable.tamilmovies);


        adapter =  new GridAdapter(this,titles,images,this);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);
        recycleview1.setLayoutManager(gridLayoutManager);
        recycleview1.setAdapter(adapter);


    }

    public void onPictureClicked() {
        Intent intent = new Intent(MainActivity2.this, MainActivity1.class);
        startActivity(intent);
    }


}

标签: androidandroid-studioandroid-layoutandroid-fragments

解决方案


您的问题是registration() 方法只设置onClickListener,它不会调用它。

似乎您没有设置 onclicklistener,即使您设置了第二个(创建 toast)也可能会覆盖它。

因此,要么调用registration()而不是另一个,要么调用另一个onClickListener内部的注册,没有onClick的东西(因为代码应该被执行,如果你在监听器内部则不需要onClick)


推荐阅读