问题
I'd like to know how to validate my 4 edittext fields if one or more of these fields are left empty after tapping the button to process the inputs. I have searched many solutions like using toast but It think it's not appropriate for multiple edittext fields and using textwatchers. I'd like the app to show a pop-up message or alert dialog box saying "Please fill up the necessary fields."
Any help would be appreciated.
回答1:
You can use below common function for checking the Null values of the edittext:
public static boolean m_isError; public static void checkEntryForEmptyValue(EditText p_editText, String p_nullMsg) { if (p_editText != null || p_nullMsg != null) { // use trim() while checking for blank values if ((p_editText.getText().toString().trim().equalsIgnoreCase("")) || (p_editText.getText().toString().trim().length() <= 0)) { m_isError = true; p_editText.setError(p_nullMsg); p_editText.requestFocus(); } } } }
Use the above function as below inside your button click listener:
CommonUtil.m_isError = false; CommonUtil.checkEntryForEmptyValue(edittext,getResources(). getString(R.string.MessageEmpty)); if (!CustomValidator.m_isError) { Toast.makeText(getApplicationContext(),"Success", Toast.LENGTH_SHORT).show(); } else { //Your dialog with the error messages. }
回答2:
u can use some tooltips for validation like qtip or poshytip
http://vadikom.com/demos/poshytip/
http://craigsworks.com/projects/qtip/
Write a validation function to check all text fields and append the tooltip object with the corresponding fields which fails the validation.
回答3:
The Simplest soulution is that check that if the fields are empty then show dialog here is simple code snippet
private void checkEntries()
{
if(!(email.getText().toString().equals("")))
{
if(!(pass.getText().toString().equals("")))
{
if(UIHelper.getInstance().emailAddressValidation(email.getText().toString()))
{
if(pass.getText().length()>=5)
{
sendLoginRequest(email.getText().toString(),pass.getText().toString(),Constants.PHONE_ID);
}
else
{
dialogBoxInUIthread("String","Password length should be greater than 5 ",LoginController.this,true);
}
}
else
{
dialogBoxInUIthread("String","Invalid Email Id",LoginController.this,true);
}
}
else
{
dialogBoxInUIthread("String","Please enter password",LoginController.this,true);
}
}
else
{
dialogBoxInUIthread("String","Please enter email",LoginController.this,true);
}
}
private void dialogBoxInUIthread(final String title,final String msg, Context context,final boolean completed) {
/* runOnUiThread(new Runnable() {
public void run() {*/
AlertDialog.Builder alertbox = new AlertDialog.Builder(LoginController.this);
alertbox.setTitle(title);
alertbox.setMessage(msg);
alertbox.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
if(completed){
}else{ }
}
});alertbox.show();
/* }
});*/
}
回答4:
Use this validate function when you click on button and you can check the alert message after method is executed
boolean flag_1= true,flag_2=true,flag_3=true;
String alertmsg;
.
private boolean validate()
{
EditText et1 = (EditText)findViewById(R.id.et1);
EditText et2 = (EditText)findViewById(R.id.et2);
EditText et3 = (EditText)findViewById(R.id.et3);
if(et1.getText().toString().isEmpty())
{
alertmsg+= "Please fill 1st\n";
flag_1 = false;
}
if(et2.getText().toString().isEmpty())
{
alertmsg+= "Please fill 2nd\n";
flag_2 = false;
}
if(et3.getText().toString().isEmpty())
{
alertmsg+= "Please fill 3rd";
flag_3 = false;
}
return flag_1||flag_2||flag_3;
}
回答5:
Try this :
EDIT:
Call this onClick of your process-input button:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.mRlayout1);
boolean success = formIsValid(rl);
if(success == false){
// alert dialog box
}
else{
// process ahead
}
Declare this function:
EDIT:
public boolean formIsValid(RelativeLayout layout) {
for (int i = 0; i < layout.getChildCount(); i++) {
View v = layout.getChildAt(i);
Class<? extends View> c = v.getClass();
if (c == EditText.class) {
EditText et = (EditText) v;
if(et.getText().toString().equals(""))
return false;
//Toast.makeText(getApplicationContext(), ""+et.getText().toString(), Toast.LENGTH_LONG).show();
}
}
return true;
}
By this you can validate N number of input controls with single call.
Thanks.
来源:https://stackoverflow.com/questions/14393442/how-to-validate-multiple-edittext-fields-with-a-pop-up-message-or-alert-dialog-b