设置按钮作为后退按钮

时间:2012-02-01 13:01:27

标签: android back-button android-button

我想设置一个简单的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();   

3 个答案:

答案 0 :(得分:2)

只需添加:

NameOfYourContainingClass.this.onBackPressed();

答案 1 :(得分: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 :(得分: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();