Android 自定义AlertDialog退出对话框
Android 自定义AlertDialog退出对话框
转 https://blog.csdn.net/wkh11/article/details/53081634
在项目中很多时候会出现点击返回键出现提示对话框。
不多说了,先看效果图
直接上代码
layout布局的名字是close_program
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="275dp"
android:layout_height="169dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/tab_rectangle" >
<TextView
android:id="@+id/tv_prompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="提示"
android:textColor="#333333"
android:textSize="16sp" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="45dp"
android:background="#25b3ff" />
<TextView
android:id="@+id/tv_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="你确定要删除吗"
android:textColor="#333333"
android:textSize="16sp" />
<View
android:id="@+id/view_liner"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_no"
android:layout_marginTop="25dp"
android:background="#e1e1e1" />
<LinearLayout
android:id="@+id/tv_cancel"
android:layout_width="90dp"
android:layout_height="40dp"
android:layout_alignTop="@+id/view1"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:layout_toLeftOf="@+id/tv_prompt" >
<TextView
android:layout_width="90dp"
android:layout_height="40dp"
android:gravity="center"
android:text="取消"
android:textColor="#25b3ff"
android:textSize="16sp" />
</LinearLayout>
<View
android:id="@+id/view1"
android:layout_width="1.5dp"
android:layout_height="48dp"
android:background="#e1e1e1"
android:layout_centerInParent="true"
android:layout_below="@+id/view_liner" />
<LinearLayout
android:id="@+id/tv_ok"
android:layout_width="90dp"
android:layout_height="40dp"
android:layout_alignTop="@+id/view1"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/tv_prompt" >
<TextView
android:layout_width="90dp"
android:layout_height="40dp"
android:gravity="center"
android:text="确定"
android:textColor="#25b3ff"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
其中有个布局的背景是圆角矩形的设置
画圆角矩形的代码 tab_rectangle
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<corners android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"/>
</shape>
Activity中的返回键的操作代码:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME){
showExitGameAlert();
}
return super.onKeyDown(keyCode, event);
}
//弹出对话框方法
private void showExitGameAlert() {
final AlertDialog dlg = new AlertDialog.Builder(this).create();
dlg.show();
Window window = dlg.getWindow();
window.setContentView(R.layout.closeprogram);
TextView tv = (TextView) window.findViewById(R.id.tv_no);
tv.setText("你确定要退出吗");
LinearLayout ok = (LinearLayout) window.findViewById(R.id.tv_ok);
//确定按钮
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
exit(); // 退出应用
}
});
//取消按钮
LinearLayout cancel = (LinearLayout) window.findViewById(R.id.tv_cancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dlg.cancel();
}
});
}
//关闭程序
private void exit() {
super.finish();
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
假设想改变Dialog的大小能够这样写:
AlertDialog dialog = getCustomDialog();
dialog.show(); //一定得在show完dialog后来set属性
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.width = AnimationTest.this.getResources().getDimensionPixelSize(R.dimen.dialog_width);
lp.height = AnimationTest.this.getResources().getDimensionPixelSize(R.dimen.dialog_height);
dialog.getWindow().setAttributes(lp);
Android 自定义AlertDialog退出对话框的更多相关文章
- Android 自定义AlertDialog(退出提示框)
有时候我们需要在游戏或应用中用一些符合我们样式的提示框(AlertDialog) 以下是我在开发一个小游戏中总结出来的.希望对大家有用. 先上效果图: 下面是用到的背景图或按钮的图片 经过查找资料和参 ...
- Android自定义扁平化对话框
平时我们开发的大多数的Android.iOS的APP,它们的风格都是拟物化设计.如今Android 4.X.iOS 7.WP8采用的是扁平化设计,可以看出扁平化设计是未来UI设计的趋势.其实扁平化设计 ...
- Android实现“是否退出”对话框和“带图标的列表”对话框
今天我们学习的内容是实现两种对话框(Dialog),第一种是询问是否退出对话框,另外一种是带图标的列表对话框,程序的执行效果是,我们点击button1的时候,弹出第一种对话框,我们点击button2的 ...
- Android自定义AlertDialog
常见的一种方法: [html] view plaincopyprint? AlertDialog.Builder builder; AlertDialog alertDialog; LayoutInf ...
- android 开发AlertDialog.builder对话框的实现
AndroidAPI提供了Dialog对话框控件,但google明确指出不建议开发者只是使用Dialog来创建对话框,而应该自定义对话框或者使用API中提供的Dialog的子类,如AlertDialo ...
- Android 自定义AlertDialog的实现
Android默认的AlertDialog太单调,我们可以通过继承原生的Dialog来实现自定义的Dialog. 本文的自定义Dialog和原生的AlertDialog的创建方式类似,通过一个静态Bu ...
- android 自定义AlertDialog(一段)
java: final AlertDialog dialog = new AlertDialog.Builder(mContext) .create(); dialog.setCancelable(f ...
- Android 自定义AlertDialog的写法和弹出软键盘和覆盖状态栏
private void showMyDialog(int layoutId){ AlertDialog myDialog = new AlertDialog.Builder(context).cre ...
- Android 自定义格式的对话框
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAaoAAADvCAIAAAAsDwfKAAAgAElEQVR4nLy9bZhcVZUofEg0GcURBk ...
随机推荐
- ASE19团队项目 beta阶段 model组 scrum6 记录
本次会议于12月9日,19时30分在微软北京西二号楼sky garden召开,持续20分钟. 与会人员:Jiyan He, Lei Chai, Linfeng Qi, Xueqing Wu, Kun ...
- 【vue开发】vue指令Vue.directive使用教程
1.指令的注册 指令跟组件一样需要注册才能使用,同样有两种方式,一种是全局注册: ? 1 2 3 4 5 Vue.directive('dirName',function(){ //定义指令 ...
- 十年阿里架构师教你如何一举拿下阿里的Offer,(附面试技巧)
前言: 现在很多人即将毕业或者换工作面临找工作,为了帮助大家,遂写下这篇文章.如果你想进入BAT,抑或拿到高工资,无论你的基础如何,你至少要花三个月时间来准备简历.笔试题.面试题.对于没有项目经验,没 ...
- windows使用zip包安装mysql8.0.12
1.前言 在windows下有两种安装mysql的方式,一种是msi的方式,一种是使用zip包的安装方式.通常都是用msi的方式,毕竟不需要敲命令,只用图形界面就可以完成安装.zip包的安装方式也很简 ...
- C# 直播录制视频
//项目引用 ffmpeg.exe 下载地址http://ffmpeg.org/ var time = DateTime.Now; ; //录制分钟 var fileName = Guid.NewGu ...
- Ubuntu系统---安NVIDIA 驱动后 CUDA+cuDNN 安装
Ubuntu系统---安NVIDIA 驱动后 CUDA+cuDNN 安装 --------------------------------------------@20190726--------- ...
- 什么是 java 序列化?(未完成)什么情况下需要序列化?(未完成)
什么是 java 序列化?(未完成)什么情况下需要序列化?(未完成)
- Java字节码整体分析与总结
上一次[https://www.cnblogs.com/webor2006/p/9508341.html]已经将编译器生成的默认构造方法的字节相关的分析完了,接下来则分析咱们自定义的方法啦,按照顺序来 ...
- Property or method "openPageOffice" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by
Property or method "openPageOffice" is not defined on the instance but referenced during r ...
- tycho 打包编译报错 Access restriction: The type XYZ is not API
解决办法: 在pom.xml中加入以下配置 <plugin> <groupId>org.eclipse.tycho</groupId> <artifactId ...