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