【发布时间】:2011-06-06 12:31:13
【问题描述】:
我是 android 开发新手,我想为编辑文本字段显示一个警告对话框。用户应该输入他的体重从10kg到99kg只有当他输入超过2位时才会出现警报对话框,并且如果我们按下测量按钮而不输入重量,它应该会显示警报对话框。请有人帮我提供示例代码。
警告对话框应包含文本和确定按钮。有什么想法吗?
【问题讨论】:
标签: android android-edittext android-alertdialog
我是 android 开发新手,我想为编辑文本字段显示一个警告对话框。用户应该输入他的体重从10kg到99kg只有当他输入超过2位时才会出现警报对话框,并且如果我们按下测量按钮而不输入重量,它应该会显示警报对话框。请有人帮我提供示例代码。
警告对话框应包含文本和确定按钮。有什么想法吗?
【问题讨论】:
标签: android android-edittext android-alertdialog
你可以这样做
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();
【讨论】:
看到这个: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();
}
}
});
【讨论】: