简体   繁体   English

空白EditText上的弹出错误

[英]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? 我如何要求用户将数据输入到EditTextEditText在填充EditText之前不允许应用程序继续运行?

Right now, my application continues to progress even after the user acknowledges the error message stating the EditText is empty and is required. 现在,即使用户确认错误消息指出EditText为空并且是必需的,我的应用程序仍继续运行。

         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 Shreyas Tallani

how to validate the phone number... enters more than 10 digits the error message should be displayed 如何验证电话号码...输入超过10位数字,应显示错误消息

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. 如果您只是想测试String的长度,只需获取String并将其长度与最大长度10相比较即可。

In your validate(...) method do something similar to the following: 在您的validate(...)方法中执行以下操作:

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. 您还可以使您的EditText仅允许数字值。 This would help you validate the numbers. 这将帮助您验证数字。 You can do this in the xml file or in code. 您可以在xml文件或代码中执行此操作。

xml 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. 你可以做的第一件事,就是添加一个validate(...)方法。 Inside validate(...) , you need to validate all the fields and if anything is left blank then show the error message and stop app progression. validate(...)内部,您需要验证所有字段,如果什么都留为空白,则显示错误消息并停止应用进度。

If all the fields are fine, then call your send method. 如果所有字段都正确,则调用您的send方法。 And send(...) should only be sending your data, not checking validation. send(...)应只发送数据,不检查验证。

You can add return statement after showing the dialog as shown below. 如下所示,您可以在显示对话框后添加return语句。

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

                            showDialog(EMPTY_TEXT_ALERT);
                            return;
                        }

It would be better to use Toast messages than showDialog though. 不过,使用Toast消息比使用showDialog更好。

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(). 我不知道您如何调用sends()方法,但是在出现任何空错误之后,您只需在showDialog()之后立即添加return语句即可。 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. 如果通过onClick()从按钮调用了send()方法,则意味着用户将看到带有错误的对话框,输入一些文本,然后单击按钮以再次发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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