2012-02-01 68 views
1

我想設置一個簡單的Alert View的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

是否要完成活動?或者你想解僱Dailog?請清除 – Arslan 2012-02-01 13:03:58

+0

我想完成當前的活動並關閉對話框,並返回上一個活動。 – MMMM 2012-02-01 13:10:28

回答

1

您必須調用完成方法才能銷燬活動。

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 2017-02-10 09:53:35

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();