用AlertDialog实现程序退出提示
来源:互联网 发布:广发证券软件 编辑:程序博客网 时间:2024/05/16 07:26
当用户使用android应用在操作过程中不小心碰到返回键,程序就关闭了。如果有什么重要的数据那就比较糟了。如果在应用点击返回时加一个提示框就会避免这样的问题了。
本次就是使用AlertDialog来实现点击返回弹出提示框效果。
源码下载
首先看效果图:
然后修改主布局文件:activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tip" /></RelativeLayout>
布局文件加了一个提示文字显示:
<resources> <string name="app_name">TestAlertDialog</string> <string name="tip">点击返回键弹出提示框</string></resources>
然后修改MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void createDialog(){ //实例话AlertDialog构建器 AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this) ; //设置提示信息 builder.setMessage("确认退出吗?"); //设置提示标题 builder.setTitle("提示"); //设置点击按钮 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //点击确定则对话框消失,并退出程序Ps(单个Activity可以用finish()结束,但多个就不行了) //多个Activity可以用System.exit(0),退出虚拟机 //还可以用android.os.Process.killProcess(android.os.Process.myPid())来结束当前进程 dialog.dismiss(); MainActivity.this.finish(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //点击取消,消失对话框 dialog.dismiss(); } }); //创建提示框并显示,注意一定要调用show()方法 builder.create().show(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0){ createDialog(); } return false; }}
首先实例化构造器对象builder,然后调用setMessage()方法设置提示信息,调用setTitle()方法设置提示标题。
最后设置按钮名称,及监听事件。
然后在在按键监听事件中调用createDialog()方法当用户点击确定是就可以退出程序了,点击取消则让提示框消失。
欢迎关注我的微信公众号,获取更多内容
更多内容请访问www.xiaob.tech
0 0
- 用AlertDialog实现程序退出提示
- 【自定义AlertDialog】退出提示框
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用设置AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- 退出程序时提示
- C# ListView控件用法
- Agilent ADS中Verilog-A学习
- 大数值——BigInteger类与BigDecimal类的概述
- OLAP及分析工具选型
- 设计模式之单例模式
- 用AlertDialog实现程序退出提示
- public <T> void show(T t),void前面的泛型T是什么作用
- 应聘技巧(所有行业都可以使用的技巧。个人看法)
- apk反编译获取完整源码 及 apk反编译后的处理
- css3 media响应式媒体查询器用法总结
- [bigdata-51] 一个高并发的 web 爬取 状态更新系统
- PyQt5系列教程(十)pyinstaller打包exe
- Homebrew/Homebrew Cask使用
- SwipeRefreshLayout(下拉刷新)与RecyclerView之简单使用<一>