android监听返回按钮事件的方法
作者:无鸯 时间:2021-12-22 04:57:57
本文实例讲述了android监听返回按钮事件的方法。分享给大家供大家参考。具体如下:
用户在点击手机的返回按钮时,默认是推出当前的activty,但是有时用户不小心按到返回,所以需要给用户一个提示,这就需要重写onkeydown事件,实现的效果如下:
java代码如下:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
protected void dialog() {
AlertDialog.Builder builder = new Builder(HanderTestActivty.this);
builder.setMessage("确定要退出吗?");
builder.setTitle("提示");
builder.setPositiveButton("确认",
new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
HanderTestActivty.this.finish();
}
});
builder.setNegativeButton("取消",
new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
dialog();
return false;
}
return false;
}
希望本文所述对大家的Android程序设计有所帮助。
标签:android,监听,事件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java事务管理学习之Hibernate详解
2023-06-07 03:14:09
![](https://img.aspxhome.com/file/2023/1/122031_0s.png)
springmvc请求转发和重定向问题(携带参数和不携带参数)
2022-09-17 13:53:59
![](https://img.aspxhome.com/file/2023/3/94223_0s.png)
C#逆变与协变详解
2021-10-25 08:07:54
![](https://img.aspxhome.com/file/2023/1/98261_0s.jpg)
Spring整合Springmvc的相关介绍
2022-03-30 10:47:08
Java如何从json字符串中获取某个值详解
2023-11-18 12:18:12
![](https://img.aspxhome.com/file/2023/6/108386_0s.png)
SpringBoot绿叶显示yml和端口问题及解决方法
2023-12-09 00:29:13
![](https://img.aspxhome.com/file/2023/5/62775_0s.png)
C# 8.0中的范围类型(Range Type)示例详解
2023-04-11 22:26:10
详解Java中的数组与字符串相关知识
2023-02-10 03:31:10
如何利用JAVA实现走迷宫程序
2022-06-23 10:52:06
![](https://img.aspxhome.com/file/2023/3/62983_0s.png)
java实现时间与字符串之间转换
2022-07-06 07:17:31
FileShare枚举的使用小结(文件读写锁)
2023-10-21 09:26:30
![](https://img.aspxhome.com/file/2023/0/100210_0s.png)
Unity通用泛型单例设计模式(普通型和继承自MonoBehaviour)
2023-08-24 14:53:28
Java基于socket实现的客户端和服务端通信功能完整实例
2023-11-22 12:12:13
![](https://img.aspxhome.com/file/2023/0/85440_0s.png)
简单了解JavaCAS的相关知识原理
2023-03-25 12:09:31
![](https://img.aspxhome.com/file/2023/9/125259_0s.png)
Spring Data JPA 设置字段默认值方式
2021-08-13 07:27:06
Java异常处理之try...catch...语句的使用进阶
2022-03-16 03:06:09
Unity Shader实现裁切效果
2023-03-05 07:54:24
![](https://img.aspxhome.com/file/2023/3/94643_0s.jpg)
详解C# 虚方法virtual
2022-11-05 09:41:17
详解Spring Aop实例之AspectJ注解配置
2023-02-04 02:52:53
![](https://img.aspxhome.com/file/2023/6/118816_0s.png)
C#难点逐个击破(8):可空类型System.Nullable
2023-07-04 14:49:40
![](https://img.aspxhome.com/file/2023/2/71742_0s.png)