2012-02-01 13 views
1

簡単なアラートビューのOKボタンを設定して、戻るボタンが機能するようにしたいと思います。どのような方法を使うべきですか?戻るボタンとして機能するボタンを設定する

AlertDialog.Builder alt_bld = new AlertDialog.Builder(this.getContext()); 
       AlertDialog alert = alt_bld.create(); 
       alert.setTitle("Your score is: " + score); 
       alert.setButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
    //here would be the "back button's method" 
}}); 
       alert.show(); 
+0

アクティビティを終了しますか?あるいは、デイログを却下したいですか?明確にしてください – Arslan

+0

私は現在の活動を終了し、ダイアログを閉じ、以前の活動に戻ることを望みます。 – MMMM

答えて

1

アクティビティを破棄するには、finishメソッドを呼び出す必要があります。

AlertDialog.Builder alt_bld = new AlertDialog.Builder(this.getContext()); 
       AlertDialog alert = alt_bld.create(); 
       alert.setTitle("Your score is: " + score); 
       alert.setButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 

        // Enter you activity Name add call finish. 

        MyAcitivity.this.finish(); // Or MyAcitivity.this.onBackPressed() 

}}); 
alert.show(); 
2

だけ追加します。そのため

NameOfYourContainingClass.this.onBackPressed(); 
+0

これは答える必要があります – Ozan

0

をちょうどこのアクティビティで&コールボタンがクリックされたとき。

@Override 
public void onBackPressed() 
{ 
    super.onBackPressed(); 
}; 



    AlertDialog.Builder alt_bld = new AlertDialog.Builder(this.getContext()); 
       AlertDialog alert = alt_bld.create(); 
       alert.setTitle("Your score is: " + score); 
       alert.setButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
       onBackPressed(); 
}}); 
       alert.show(); 
関連する問題