2017-05-16 7 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

uは、このダイアログ –

+0

そして、どのようにそれを初期化するための初期化コードの一部を示して?私はちょうどメソッドshowDialog(...)を呼び出しています。 – Tenday

+0

おかげで、私が見つかりました:ダイアログ= adb.createを(); – Tenday

答えて

1
this.dismiss(); 

メソッド終了はアクティビティ用です。 と>>この < <ここでは、(対話クラス内の問題のための)対話のインスタンスです。

とあなたが対話クラスにアクティビティインスタンスを渡す必要が対話から活動を終了したい場合。

+0

私はちょうど方法にShowDialog(...)を呼んでいます。ダイアログを初期化する方法は?カッコ内にパラメータとして含める必要があるもの:dialog = new AlertDialog(...); – Tenday

+0

私はこれを書くとき: "ダイアログ=新しいAlertDialog(これ);" AlertDialog(android.content.Context) 'android.app.AlertDialogのアクセスを保護しました – Tenday

+0

ありがとう、私は見つかりました:ダイアログ= adb.create(); – Tenday

関連する問題