简体   繁体   中英

Popup Error on blank EditText

How do I require the user to input data into an EditText and not allow the application to proceed until the EditText is populated?

Right now, my application continues to progress even after the user acknowledges the error message stating the EditText is empty and is required.

         private final static int EMPTY_TEXT_ALERT = 0;

    protected Dialog onCreateDialog(int id) {

    switch(id) {

        case EMPTY_TEXT_ALERT: {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("oops!!")
                   .setPositiveButton("ok", new DialogInterface.OnClickListener() {

                       @Override
                       public void onClick(DialogInterface dialog, int which) {
                           dialog.cancel();
                       }
             });

             return builder.create();
        }
    }

    return null;


}




        public void sends(View v) {   

            DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);    

            int year = datePicker.getYear();
            int month = datePicker.getMonth();
            int day = datePicker.getDayOfMonth();
                 final EditText phone = (EditText) findViewById(R.id.editText2); 

            final EditText nameplate = (EditText) findViewById(R.id.editText3);  

            final EditText issue = (EditText) findViewById(R.id.editText4);  



                     String ph = phone.getText().toString();
                     if(ph.trim().equals("")) {
                            // text is empty

                            showDialog(EMPTY_TEXT_ALERT);

                        }
                    String np = nameplate.getText().toString();
                    if(np.trim().equals("")) {
                        // text is empty

                        showDialog(EMPTY_TEXT_ALERT);
                    }
                     String i = issue.getText().toString(); 
                     if(i.trim().equals("")) {
                            // text is empty

                            showDialog(EMPTY_TEXT_ALERT);
                        }


                     else
                         {StringBuilder s= new StringBuilder(100);
            s.append(year);
            s.append(". ");
            s.append(month+1);// month starts from 0 in this
            s.append(". ");
            s.append(day);
            s.append(". ");
            s.append(". ");
            s.append(ph);
            s.append(". ");
            s.append(np);
            s.append(". ");
            s.append(i);

            String st=s.toString();


            Intent emailIntentt = new Intent(android.content.Intent.ACTION_SEND); 
            emailIntentt.setType("plain/text");


            String aEmailList[] = { "shreyas.t@gmail.com" };  


            emailIntentt.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);  

            emailIntentt.putExtra(android.content.Intent.EXTRA_SUBJECT, "Feedback");  


            emailIntentt.putExtra(android.content.Intent.EXTRA_TEXT, st);


            startActivity(emailIntentt); 
        } 

}}

Shreyas Tallani

how to validate the phone number... enters more than 10 digits the error message should be displayed

If you are just wanting to test the length of the String , just get the String and compare the length to the max length of 10.

In your validate(...) method do something similar to the following:

String ph = phone.getText().toString();
if(ph.trim().equals("")) {
    showDialog(EMPTY_TEXT_ALERT);
} else if (ph.length() > 10) {
    showDialog(TEXT_TOO_LONG_ALERT);
}

You could also make your EditText only allow numeric values. This would help you validate the numbers. You can do this in the xml file or in code.

xml

android:inputType="TYPE_NUMBER_VARIATION_NORMAL"

code

EditText.setInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL);

First thing you can do, is add a validate(...) method. Inside validate(...) , you need to validate all the fields and if anything is left blank then show the error message and stop app progression.

If all the fields are fine, then call your send method. And send(...) should only be sending your data, not checking validation.

You can add return statement after showing the dialog as shown below.

if(i.trim().equals("")) {
                            // text is empty

                            showDialog(EMPTY_TEXT_ALERT);
                            return;
                        }

It would be better to use Toast messages than showDialog though.

I don't know how you are calling your sends() method, but after any empty error you can just add a return statement immediately after the showDialog(). It means that somehow the sends() method has to get re-invoked via the UI after the user has put in text.

If your sends() method is called from a button via onClick(), then it means the user will see dialog with error, input some text and then, hit the button to send again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM