安卓:code处理软键返回按钮或返回按钮是在移动 [英] Android: code to handle return button of the softkey or return button which is on mobile

查看:114
本文介绍了安卓:code处理软键返回按钮或返回按钮是在移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序由客户端收集一些数据。在主layout.xml客户把一些数据,并在最后,他们需要点击保存按钮后,所有的数据将在数据库中或其他地方存放。我想要什么,当客户端在应用程序的中间,当他美元返回按钮或后退按钮p $ PSS则一个警告框会弹出该展会的数据将丢失你想干什么是或否。
在点击是应用程序将停止并进入主屏幕,如果他美元,否按钮p $ PSS然后将其返回到当前布局在客户端输入数据。

in my application I collect some data by the client. On the main layout.xml client put some data and in last they need to click on save button after that all the data will store in database or in other place. What I want when the client is in middle of the application and when he press on return button or back button then one alert box will popup that show data will loss what do you want "Yes" or "No". on clicking on "Yes" application will stop and go to the home screen and if he press on "No" button then it return to the current layout where the client entering the data.

请给code这个问题。

Please give the code for this problem.

推荐答案

覆盖的方法的 onBack pressed 内的活动,以实现这一目标。

override the method onBackPressed inside your Activity to achieve this

@Override
public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to exit?")
           .setCancelable(false)
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    CustomTabActivity.this.finish();
               }
           })
           .setNegativeButton("No", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
               }
           });
    AlertDialog alert = builder.create();
    alert.show();

}

这篇关于安卓:code处理软键返回按钮或返回按钮是在移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆