Android 当“活动”中有大量编辑文本时,如何验证编辑文本

Android 当“活动”中有大量编辑文本时,如何验证编辑文本,android,android-layout,Android,Android Layout,当活动中有许多编辑文本使用单个函数时,如何验证编辑文本。在我的活动中,我为每个编辑文本代码做了修改,但这是一个很长的过程。 有没有其他方法可以轻松做到这一点 if (first_name1.isEmpty()) { Toast.makeText(getBaseContext(), "Please fill the First Name field !!! ", Toast.LENGTH_SHORT) .show();

当活动中有许多编辑文本使用单个函数时,如何验证编辑文本。在我的活动中,我为每个编辑文本代码做了修改,但这是一个很长的过程。 有没有其他方法可以轻松做到这一点

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;
    }
创建一个空方法()

传递您想要的字符串值和消息。
每次都可以调用此方法进行验证

您可以像这样在
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

调用此函数。ohk在其中获取一个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;

    }

}
公共类MainActivity扩展活动{
@凌驾
创建时受保护的void(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 editarray=新的ArrayList();
editarray.add(e1);
editarray.add(e2);
editarray.add(e3);
editarray.add(e4);
editarray.add(e5);
editarray.add(e6);
testingemptyedit(editarray);
}
布尔TestingImptyEdit(ArrayList editarray)
{

对于(int i=0;i请尝试此选项以检查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
            }
        }
    }
公共无效检查(编辑文本…编辑)
{
对于(int i=0;i您可以尝试:

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);
请尝试以下代码:

  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;
        }

如果在
EditText
中没有输入数据,则
EditText.getText()
返回null,因此
EditText.getText().toString()
可以抛出
NullPointerException
。相反,在
If()
中检查应为
TextUtils.isEmpty(EditText.getText())
这减少了
异常的可能性
@aduaitpokhryyal now chk dude
test.getText().toString().trim()
只应是
test.getText()
作为调用
toString()
如果在EditText`中没有输入数据,也会导致
NullPointerException
。它的r8 dude chk wd your end对于TextUtils都是相同的
  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;
        }