开发者

Edit text with alert dialog box

I am new to android development and I want to show an alert dialog box for an edit text field. User should enter his weight from 10kg to 99kg only if he enters more than 2 digits alert dialog box should appear, and also with out entering the weight if we press the measure Button it should show the alert dialog. plz some one help me with sample code.

The alert dialog should contain both text a开发者_Go百科nd OK button. Any Idea?


You can do something like this

AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Weight");
alertDialog.setMessage("You forgot to enter your weight!");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int which) {
      // do something when the user presses OK (place focus on weight input?)
   }
});
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();


See this: Alert Dialog Builder


EditText eText = (EditText)findViewById(----id of the editText---);
Button okButton = (Button) findViewById(----id of the button);
AlertDialog aDialog = new AlertDialog(context);
aDialog.setTitle("Alert!!!");
aDialog.setButton(AlertDialog.BUTTON_POSITIVE,"Ok","");

okButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
String weightString = eText.getText();
if(weightString.equals("")||weightString == null)
{
aDialog.setMessage("You forgot to enter your weight");
aDialog.show();
}
else if(Integer.parseInt(weightString)>99)
{
aDialog.setMessage("Enter a weight less than 100");
aDialog.show();
}
}
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜