android studio 退出提示,Android实现退出提示的功能

摘要:本文主要是实现在Android中退出提示的功能,平常使用Android手机时,点击返回键时,由于不小心点击返回键过快,导至程序返回试界面之后直接退出程序,导至用户还要再重新登录一次。为解决此类问题,我们需要在返回到主界面中,拦截手机的返回键功能,进行提示用户是否退出当前程序,以些达到用户不小心点击返回键时直接退出应用程序(转自:www.yunbook.vip/post/154388…)。

一、实现步骤

1、在Activity中进行 finish() 函数的重写

@Override

public void finish() {

// 重写

}

2、AlertDialog对话框的创建,用于提示对话框。

// 创建对话框

AlertDialog.Builder dialog = new AlertDialog.Builder(this);

3、设置对话框的标题、内容和其它参数

dialog.setTitle("提示");

dialog.setMessage("是否退出当前程序?");

4、创建对话框的 "确定" 和 "取消" 按钮

dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialogInterface, int i) {

System.exit(0);

}

});

dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialogInterface, int i) {

Log.d("dialog", "___取消");

}

});

5、对话框显示

if (dialog != null) {

dialog.show();

}

二、实现源码如下:

/** 主界面 */

public class Main extends Activity {

/** 标签 */

private static final String TAG = "Main";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.act_main);

}

@Override

protected void onResume() {

super.onResume();

}

@Override

protected void onPause() {

super.onPause();

}

@Override

protected void onDestroy() {

super.onDestroy();

}

@Override

public void finish() {

AlertDialog.Builder dialog = new AlertDialog.Builder(this);

dialog.setTitle("提示");

dialog.setMessage("是否退出当前程序?");

dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialogInterface, int i) {

System.exit(0);

}

});

dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialogInterface, int i) {

Toast.makeText(Main.this, "取消", Toast.LENGTH_LONG).show();

Log.d(TAG, "___取消");

}

});

if (dialog != null) {

dialog.show();

}

}

复制代码

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值