如何使用单一功能在活动中编辑大量编辑文本时验证编辑文本。我通过制作每个编辑文本代码来完成我的活动,但这是一个非常长的过程。 有没有其他方法可以轻松地思考这个问题?
if (first_name1.isEmpty()) {
Toast.makeText(getBaseContext(),
"Please fill the First Name field !!! ", Toast.LENGTH_SHORT)
.show();
first_name.requestFocus();
first_name.setText("");
return 1;
}
sur_name1 = sur_name.getText().toString();
if (sur_name1.isEmpty()) {
Toast.makeText(getBaseContext(),
"Please fill the sur name field !!! ", Toast.LENGTH_SHORT)
.show();
// sur_name.setFocusable(true);
sur_name.setText("");
sur_name.requestFocus();
return 1;
}
middle_name1 = middle_name.getText().toString();
if (middle_name1.isEmpty()) {
Toast.makeText(getBaseContext(),
"Please fill the middle_name field !!! ",
Toast.LENGTH_SHORT).show();
middle_name.setText("");
middle_name.requestFocus();
return 1;
}
mother_name1 = mother_name.getText().toString();
if (mother_name1.isEmpty()) {
Toast.makeText(getBaseContext(),
"Please fill the mother name field !!! ",
Toast.LENGTH_SHORT).show();
mother_name.requestFocus();
return 1;
}
date_of_birth1 = date_of_birth.getText().toString();
if (date_of_birth1.isEmpty()) {
Toast.makeText(getBaseContext(),
"Please fill the date of birth field !!! ",
Toast.LENGTH_SHORT).show();
date_of_birth.requestFocus();
return 1;
}
place_of_birth1 = place_of_birth.getText().toString();
if (place_of_birth1.isEmpty()) {
Toast.makeText(getBaseContext(),
"Please fill the place of birth field !!! ",
Toast.LENGTH_SHORT).show();
place_of_birth.requestFocus();
return 1;
}
blood_group1 = blood_group.getText().toString();
if (blood_group1.isEmpty()) {
Toast.makeText(getBaseContext(),
"Please fill the blood group field !!! ",
Toast.LENGTH_SHORT).show();
blood_group.requestFocus();
return 1;
}
religion1 = religion.getText().toString();
if (religion1.isEmpty()) {
Toast.makeText(getBaseContext(),
"Please fill the religion field !!! ", Toast.LENGTH_SHORT)
.show();
religion.requestFocus();
return 1;
}
local_address1 = local_address.getText().toString();
if (local_address1.isEmpty()) {
Toast.makeText(getBaseContext(),
"Please fill the local address field !!! ",
Toast.LENGTH_SHORT).show();
local_address.requestFocus();
return 1;
}
local_street1 = local_street.getText().toString();
if (local_street1.isEmpty()) {
Toast.makeText(getBaseContext(),
"Please fill the local street field !!! ",
Toast.LENGTH_SHORT).show();
local_street.requestFocus();
return 1;
}
local_taluka1 = local_taluka.getText().toString();
if (local_taluka1.isEmpty()) {
Toast.makeText(getBaseContext(),
"Please fill the taluka field !!! ", Toast.LENGTH_SHORT)
.show();
local_taluka.requestFocus();
return 1;
}
local_state1 = local_state.getText().toString();
if (local_state1.isEmpty()) {
Toast.makeText(getBaseContext(),
"Please fill the state field !!! ", Toast.LENGTH_SHORT)
.show();
local_state.requestFocus();
return 1;
}
答案 0 :(得分:0)
创建方法Empty();
Public String Empty(String val,String msg){
if (val.isEmpty()) {
Toast.makeText(getBaseContext(),
msg,Toast.LENGTH_SHORT).show();
}
将您想要的字符串值和消息传递给Toast。 每次都可以调用此方法进行验证。
答案 1 :(得分:0)
您可以像这样在Utils
课程中编写一个函数。
public static final boolean isValidEditText(EditText edt, String mString) {
String hasSomeText = edt.getText().toString();
int mLength = hasSomeText.length();
if (mLength > 0) {
return true;
// There is some value in the edit text
} else {
// Ther is no value in your edit text || Show a toast message.
Toast.makeText(YourActivity.this,
"Please fill the" + mString + " field !!! ",
Toast.LENGTH_SHORT).show();
edt.requestFocus();
edt.setText("");
return false;
}
}
您必须为所有EditText
调用此函数。
答案 2 :(得分:0)
哦,拿一个xml,然后拿一些编辑文本做这样的
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText e1 = (EditText) findViewById(R.id.a);
EditText e2 = (EditText) findViewById(R.id.b);
EditText e3 = (EditText) findViewById(R.id.c);
EditText e4 = (EditText) findViewById(R.id.d);
EditText e5 = (EditText) findViewById(R.id.e);
EditText e6 = (EditText) findViewById(R.id.f);
ArrayList<EditText> editarray = new ArrayList<EditText>();
editarray.add(e1);
editarray.add(e2);
editarray.add(e3);
editarray.add(e4);
editarray.add(e5);
editarray.add(e6);
testingemptyedit(editarray);
}
boolean testingemptyedit(ArrayList<EditText> editarray)
{
for (int i = 0 ; i<editarray.size();i++)
{
EditText test = editarray.get(i);
if(TextUtils.isEmpty(test.getText().toString().trim()))
{
System.out.println("and the empty edit text is is == " + test);
return false;
}
}
return true;
}
}
答案 3 :(得分:0)
尝试使用此方法检查EditText是否为空:
public void check (EditText...edit)
{
for(int i=0; i<edit.length; i++)
{
if(!TextUtils.isEmpty(edit[i].getText().toString()))
{
//Do what you want
}
}
}
答案 4 :(得分:0)
您可以尝试:
public void validateEditText(EditText mEditText, String toastMsg, Context mContext) {
if (TextUtils.isEmpty(mEditText.getText())) {
Toast.makeText(mContext, toastMsg, Toast.LENGTH_SHORT).show();
mEditText.setText("");
mEditText.requestFocus();
}
}
将此方法称为:
EditText toCheck = (EditText)findViewById(R.id.editTextToCheck);
validateEditText(toCheck , "Please fill data in EditText toCheck!! ", ThisActivity.this);
答案 5 :(得分:0)
尝试以下代码:
if (validateInputs()) {
//all fields are fill
}
/**
* validate whether user has entered all the inputs.
*/
private boolean validateInputs() {
if (first_name.getText().toString().trim().equalsIgnoreCase("")) {
Toast.makeText(YourActiivtyName.this, "Please enter First Name",
Toast.LENGTH_LONG).show();
return false;
}
if (sur_name.getText().toString().trim().equalsIgnoreCase("")) {
Toast.makeText(YourActiivtyName.this, "Please enter SurName ",
Toast.LENGTH_LONG).show();
return false;
}
if (mother_name.getText().toString().trim().equalsIgnoreCase("")) {
Toast.makeText(YourActiivtyName.this, "Please enter Mother Name",
Toast.LENGTH_LONG).show();
return false;
}
if (date_of_birth.getText().toString().trim().equalsIgnoreCase("")) {
Toast.makeText(YourActiivtyName.this,
"Please enter dateof Birth", Toast.LENGTH_LONG)
.show();
return false;
}
if (place_of_birth.getText().toString().trim().equalsIgnoreCase("")) {
Toast.makeText(YourActiivtyName.this,
"Please enter Place of Birth", Toast.LENGTH_LONG)
.show();
return false;
}
return true;
}