android - 单击按钮验证不起作用
问题描述
所以基本上我已经在我有验证和意图中创建了一个登录表单..但是问题来了是页面是预期的..但是验证不起作用.....就像我将表单清空吐司一样正在显示味精,但页面也在预期中......我不想要这样......我想要如果所有字段都是空的,那么页面不应该意图并且应该进行验证。
这是我的 NewUserActivity.java 代码
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();
}
});
Intent p = new Intent(NewUserActivity.this,MainActivity2.class);
startActivity(p);
}
}
这是我的 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:text="REGISTER"
android:textColor="@color/Black"
android:textSize="24dp" />
</RelativeLayout>
这是我的 Toastmanager.class
public class ToastManager {
private final WeakReference<Context> mContext;
private final Handler uiHandler = new Handler(Looper.getMainLooper());
private final List<Item> items = new ArrayList<>();
private int durationInMillis;
private boolean isShowing;
private int delayedBetweenToastInMillis;
public ToastManager(Context context) {
mContext = new WeakReference<>(context);
}
public void addToast(String message, @NonNull Duration duration) {
Item item = new Item(message, duration);
items.add(item);
}
public void show() {
// Prevent client from calling this method many time.
if (isShowing) {
return;
}
// Show all toast on screen.
showToast();
// After calling show(), if client add new toasts by calling addToast()
// Then we must show them on screen. Otherwise reset all data of this class.
uiHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (!items.isEmpty()) {
showToast();
} else {
reset();
}
}
}, durationInMillis);
}
public void setDelayedBetweenToast(int delayInMillis) {
delayedBetweenToastInMillis = delayInMillis;
}
public void cancel() {
reset();
uiHandler.removeCallbacksAndMessages(null);
}
private void showToast() {
List<Item> list = new ArrayList<>(items);
items.clear();
durationInMillis = 0;
for (final Item item : list) {
uiHandler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(mContext.get(), item.text, item.getDurationForToast()).show();
}
}, durationInMillis);
durationInMillis += item.getDurationInMillis() + delayedBetweenToastInMillis;
}
}
private void reset() {
items.clear();
durationInMillis = 0;
isShowing = false;
}
private static class Item {
String text;
Duration duration;
Item(String text, Duration duration) {
this.text = text;
this.duration = duration;
}
int getDurationForToast() {
return duration == Duration.LENGTH_SHORT ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG;
}
int getDurationInMillis() {
return duration == Duration.LENGTH_SHORT ? 2000 : 3500;
}
}
enum Duration {
LENGTH_SHORT,
LENGTH_LONG
}
}
解决方案
根本原因
在onCreate()
方法中NewUserActivity
,你总是开始一个新的活动:
Intent p = new Intent(NewUserActivity.this, MainActivity2.class);
startActivity(p);
这就解释了为什么第一次打开应用程序时,您总是会看到MainActivity2
.
解决方案
当用户点击“注册”按钮时:
如果所有注册字段(共 5 个)均有效,则显示
MainActivity2
否则,在屏幕上显示 Toast
这是新代码(请看我的代码注释以了解逻辑代码)
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 isAtLeastOneFieldNotEmpty = !NAME.isEmpty()
|| !EMAIL.isEmpty()
|| !PHONENO.isEmpty()
|| !username.isEmpty()
|| !password.isEmpty();
toastManager = new ToastManager(NewUserActivity.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;
}
// 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);
} 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(NewUserActivity.this, MainActivity2.class);
startActivity(p);
}
}
});
}
}
推荐阅读
- python - 如何在Python中删除两个双括号之间的文本
- clickhouse - Superset 无法访问 ClickHouse 数据库
- javascript - 无法访问从 fetch api 请求中获取结果
- macos - 在 MacOS 上运行 Makefile 时出现部分错误
- powershell - PowerShell 7.0 和 Windows PowerShell 5.1 Get-Member 输出与服务对象
- c++ - 如何在 Eclipse 中正确使用 gmp-mpir-mpfr?
- reactjs - 为什么我在 componentDidMount 中的函数没有被触发?
- linux - 系统重启后重新连接到控制台输出
- python - Sympy:指定函数的导数
- javascript - 使用引导程序显示带有来自 javascript 的两个按钮的确认对话框