开发者

How to alert when user submits with empty fields

I want to add an alert in my code, so that when the user clicks the button without filling fields, an error message occurs. Please suggest some measures.

This is my code:

    Button button1 = (Button) findViewById(R.id.widget45);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

          tv= name.getText().toString();
           tv1 = pass.getText().toString();
           x.setText(tv);
           y.setText(tv1);开发者_高级运维
       }
    });


}

}


You could do something like:

Button button1 = (Button) findViewById(R.id.widget45);
button1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

      tv= name.getText().toString();
      tv1 = pass.getText().toString();
      if( tv.length() == 0 || tv1.length() == 0 ) {
          Toast.makeText(v.getContext(), R.string.input_not_filled, Toast.LENGTH_SHORT).show();
          return;
      }
      x.setText(tv);
      y.setText(tv1);
   }
});

This checks if any of the returning string is empty. If so, toasts a small message with the text saved in string.xml named input_not_filled for 1 second. And of course doesn't process the setText. Of course you could check the elements independently.

I think this is the easiest way.

Cheers


Ok, try using AlertDialog. Check if the fields are empty and if so,

final AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext())
                    .create();
alertDialog.setTitle("Warning");
alertDialog.setMessage("Please fill required fields");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    alertDialog.dismiss();
                    //Warn user
                }
            });
alertDialog.show();

Even though i think it would be even better to use Toast + Animation as the Google example shows us. No need to bother user.


Hey Welcome to Stackoverflow.

This site is intended to help you discover your errors and fix them with the help of other users. Therefore it is also helping, if you don't let out each a, i, e, o or u.

Now to your problem.

To check, whether a field is empty, you test like the following:

tv = name.getText().toString();
if(tv.trim().equals("")) {
    // text is empty

    showDialog(EMPTY_TEXT_ALERT);
}

Now, that you have called showDialog you have to override onCreateDialog(int id) in your activity class. You also have to define the EMPTY_TEXT_ALERT variable I used here.

private final static int EMPTY_TEXT_ALERT = 0;

@Override
protected Dialog onCreateDialog(int id) {
    switch(id) {
        case EMPTY_TEXT_ALERT: {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Your error message")
                   .setPositiveButton("OK", new DialogInterface.OnClickListener() {

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

             return builder.create();
        }
    }

    return null;
}

This way a call to showDialog with the appropriate id will show the dialog and a click on the OK-Button, that will be created closes it afterwards.


This is easy and works fine

EditText ed=(EditText)findViewById(R.id.ed1);
String var=ed.getText().toString();
if(var.length()==0) {
   ed.setError("Your message");
}
else {
   // continue
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜