2011-06-06 55 views
1

我是android开发新手,我想为编辑文本字段显示一个警告对话框。只有当他输入超过2位数的警报对话框时,用户才应该输入他的体重从10公斤到99公斤,并且如果我们按下测量按钮并且没有输入重量按钮,它应该显示警报对话框。请帮我看一下示例代码。编辑带有提醒对话框的文本

警报对话框应包含文本和确定按钮。任何想法?

回答

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