在许多已经存在的Android应用程序中,当涉及到表单时,其中会包含用户详细信息。如果用户在文本字段中输入了错误的信息,或者用户未填写文本而离开了文本字段,则需要向未填充且包含错误信息的TextField提供某些警报信息。因此,在本文中,已逐步讨论了如何将错误文本提供给用户。请看以下图片,以了解在此讨论中必须实现的内容。注意,我们将使用Java语言实现该项目。
在此讨论中,出于演示目的采取了两个示例活动,因为在第一个活动中实现了文本字段。如果在文本字段中输入的所有数据均符合要求,则用户应继续进行下一个活动。
在Android中实施表单验证的步骤
步骤1:创建一个空的活动项目
- 创建一个空的活动Android Studio项目。并将编程语言选择为Java 。
- 参考Android |如何在Android Studio中创建/启动新项目?知道如何创建一个空的活动android studio项目。
步骤2:使用activity_main.xml
- 在本例中,出于演示目的,仅实现了四个文本字段,即名字,姓氏,电子邮件和密码。
- 在activity_main.xml文件中调用以下代码。
XML
XML
Java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
// two buttons
Button bCancel, bProceed;
// four text fields
EditText etFirstName, etLastName, etEmail, etPassword;
// one boolean variable to check whether all the text fields
// are filled by the user, properly or not.
boolean isAllFieldsChecked = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// register buttons with their proper IDs.
bProceed = findViewById(R.id.proceedButton);
bCancel = findViewById(R.id.cancelButton);
// register all the EditText fields with their IDs.
etFirstName = findViewById(R.id.firstName);
etLastName = findViewById(R.id.lastName);
etEmail = findViewById(R.id.email);
etPassword = findViewById(R.id.password);
// handle the PROCEED button
bProceed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// store the returned value of the dedicated function which checks
// whether the entered data is valid or if any fields are left blank.
isAllFieldsChecked = CheckAllFields();
// the boolean variable turns to be true then
// only the user must be proceed to the activity2
if (isAllFieldsChecked) {
Intent i = new Intent(MainActivity.this, MainActivity2.class);
startActivity(i);
}
}
});
// if user presses the cancel button then close the
// application or the particular activity.
bCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.finish();
System.exit(0);
}
});
}
// function which checks all the text fields
// are filled or not by the user.
// when user clicks on the PROCEED button
// this function is triggered.
private boolean CheckAllFields() {
if (etFirstName.length() == 0) {
etFirstName.setError("This field is required");
return false;
}
if (etLastName.length() == 0) {
etLastName.setError("This field is required");
return false;
}
if (etEmail.length() == 0) {
etEmail.setError("Email is required");
return false;
}
if (etPassword.length() == 0) {
etPassword.setError("Password is required");
return false;
} else if (etPassword.length() < 8) {
etPassword.setError("Password must be minimum 8 characters");
return false;
}
// after all validation return true.
return true;
}
}
输出界面:
步骤3:建立另一个空白的活动
- 使用activity_main2.xml创建另一个空的活动,并调用以下代码,其中包含简单的文本“ Activity 2”,以避免混淆。
- 仅当用户在第一个活动中给定的文本字段中正确输入数据时,用户才应继续执行此活动。
XML格式
步骤4:使用MainActivity。 Java文件
- 在这里,对于EditText类的实例,将调用setError() 。
When the data filled in the text filed is wrong ->
// this gives error message to perticular text fild
// which contains the wrong data.
editText1.setError(“Error message”);
When user data is corrected by the user ->
// There is no need to give the error message when the user
// corrects wrongly entered text field.
editText1.setError(null);
- 调用以下代码。添加了注释以便更好地理解。
Java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
// two buttons
Button bCancel, bProceed;
// four text fields
EditText etFirstName, etLastName, etEmail, etPassword;
// one boolean variable to check whether all the text fields
// are filled by the user, properly or not.
boolean isAllFieldsChecked = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// register buttons with their proper IDs.
bProceed = findViewById(R.id.proceedButton);
bCancel = findViewById(R.id.cancelButton);
// register all the EditText fields with their IDs.
etFirstName = findViewById(R.id.firstName);
etLastName = findViewById(R.id.lastName);
etEmail = findViewById(R.id.email);
etPassword = findViewById(R.id.password);
// handle the PROCEED button
bProceed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// store the returned value of the dedicated function which checks
// whether the entered data is valid or if any fields are left blank.
isAllFieldsChecked = CheckAllFields();
// the boolean variable turns to be true then
// only the user must be proceed to the activity2
if (isAllFieldsChecked) {
Intent i = new Intent(MainActivity.this, MainActivity2.class);
startActivity(i);
}
}
});
// if user presses the cancel button then close the
// application or the particular activity.
bCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.finish();
System.exit(0);
}
});
}
// function which checks all the text fields
// are filled or not by the user.
// when user clicks on the PROCEED button
// this function is triggered.
private boolean CheckAllFields() {
if (etFirstName.length() == 0) {
etFirstName.setError("This field is required");
return false;
}
if (etLastName.length() == 0) {
etLastName.setError("This field is required");
return false;
}
if (etEmail.length() == 0) {
etEmail.setError("Email is required");
return false;
}
if (etPassword.length() == 0) {
etPassword.setError("Password is required");
return false;
} else if (etPassword.length() < 8) {
etPassword.setError("Password must be minimum 8 characters");
return false;
}
// after all validation return true.
return true;
}
}