Android 设置一个按钮作为后退按钮

Android 设置一个按钮作为后退按钮,android,back-button,android-button,Android,Back Button,Android Button,我想设置一个简单的警报视图的OK按钮作为后退按钮。我应该用什么方法 AlertDialog.Builder alt_bld = new AlertDialog.Builder(this.getContext()); AlertDialog alert = alt_bld.create(); alert.setTitle("Your score is: " + score); alert.setButt

我想设置一个简单的警报视图的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();   
只需添加:

NameOfYourContainingClass.this.onBackPressed();

为此,请在活动中单击此按钮并调用

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

您必须调用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();   

您想完成活动吗?还是要解雇Dailog?请注意,我想完成当前活动并关闭对话框,然后返回到上一个活动。这应该是答案