2017-05-16 65 views
0

我有我自己的標記一個對話框:如何分配一個按鈕來關閉對話框?

@Override 
protected Dialog onCreateDialog(int id) { 
    AlertDialog.Builder adb = new AlertDialog.Builder(this); 
    LayoutInflater inflater = this.getLayoutInflater(); 
    View view = inflater.inflate(R.layout.dialog_menu, null); 
    title = (TextView) view.findViewById(R.id.title); 
    text = (TextView) view.findViewById(R.id.textMenu); 
    btnOk = (Button) view.findViewById(R.id.btnOk); 
    btnCancel = (Button) view.findViewById(R.id.btnCancel); 
    title.setText(R.string.end_game); 
    text.setText(R.string.end_text); 
    btnOk.setText(R.string.btnOk); 
    btnOk.setOnClickListener(this); 
    btnCancel.setText(R.string.btnCancel); 
    btnCancel.setOnClickListener(this); 
    if (id == DIALOG_EXIT) { 
     title.setText(R.string.main_menu); 
     text.setText(R.string.menu_text); 
     adb.setView(view); 
     return adb.create(); 
    } 
    if (id == DIALOG_END){ 
     title.setText(R.string.end_game); 
     text.setText(R.string.end_text); 
     adb.setView(view); 
     return adb.create(); 
    } 
    return super.onCreateDialog(id); 
} 

我怎樣才能關閉對話框,當我點擊按鈕「btnCancel」?在這種情況下finish()方法無法正常工作。

+0

顯示你初始化這個對話框的代碼部分 –

+0

以及如何初始化它?我只是調用一個方法showDialog(...); – Tenday

+0

謝謝,我發現:dialog = adb.create(); – Tenday

回答

1
this.dismiss(); 

該方法完成是針對活動的。 和>> < <這裏是對話的實例(爲您的對話類內的問題)。

如果您想完成對話活動,您需要將活動實例傳遞給對話類。

+0

我只是調用方法showDialog(...);以及如何初始化對話框?括號中應該包含什麼參數:dialog = new AlertDialog(...); – Tenday

+0

當我這樣寫:「dialog = new AlertDialog(this);」我有一個題詞:AlertDialog(android.content.Context)'已經在'android.app.AlertDialog – Tenday

+0

保護訪問謝謝,我發現:dialog = adb.create(); – Tenday

相關問題