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

发布于 2024-12-31 23:57:26 字数 498 浏览 4 评论 0原文

我想设置一个简单的警报视图的确定按钮来充当后退按钮的作用。我应该使用什么方法?

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

I would like to setup a simple Alert View's OK button to act as the back button acts. What method should I use?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

国粹 2025-01-07 23:57:26

只需添加:

NameOfYourContainingClass.this.onBackPressed();

Just add:

NameOfYourContainingClass.this.onBackPressed();
疧_╮線 2025-01-07 23:57:26

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

You have to call the finish method in order to destroy activity.

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();   
归属感 2025-01-07 23:57:26

为此,您的活动和活动中仅此而已单击按钮时调用。

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

for that just this in your activity & call when button is clicked.

@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();  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文