使用警告对话框编辑文本

时间:2011-06-06 12:31:13

标签: android android-edittext alertdialog

我是Android开发的新手,我想为编辑文本字段显示一个警告对话框。用户只有在输入超过2位数的警报对话框时才应输入10kg至99kg的重量,如果我们按下测量按钮则输入重量按钮应显示警告对话框。 PLZ有人帮我提供示例代码。

警告对话框应包含文本和确定按钮。有什么想法吗?

3 个答案:

答案 0 :(得分:4)

你可以做这样的事情

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();

答案 1 :(得分:1)

请参阅:Alert Dialog Builder

答案 2 :(得分:0)

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();
}
}
});