我想用OK和Cancel按鈕顯示一個提醒對話框,但我只能取消。當我評論取消按鈕,然後我得到確定按鈕。奇怪的。總之,這裏的代碼:AlertDialog ...沒有確定按鈕,只有取消
final AlertDialog ukucajIme = new AlertDialog.Builder(Kviz.this).create();
ukucajIme.setTitle("Insert your name");
final EditText input = new EditText(Kviz.this);
ukucajIme.setView(input);
ukucajIme.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable ukucanoIme = input.getText();
finish();
}
});
ukucajIme.setButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
ukucajIme.dismiss();
finish();
}
});
ukucajIme.show();
而且問題二:每當我嘗試使用類似ukucajIme.setPositiveButton它給了我一個錯誤,並表示將其更改爲SET按鈕。這是爲什麼?使用方法setPositiveButton()
和setNegativeButton()
在AlertDialogBuilder這樣
AlertDialog.Builder builder = new AlertDialog.Builder(Kviz.this);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Editable ukucanoIme = input.getText();
finish();
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
你打電話.create()太早。按照下面的答案,然後創建AlertDialog – ElefantPhace 2013-03-07 18:37:41