服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android使用自定义alertdialog实现确认退出按钮

Android使用自定义alertdialog实现确认退出按钮

2021-05-18 19:58一叶飘舟 Android

本文通过实例代码给大家详解Android使用自定义alertdialog实现确认退出按钮,对alertdialog退出按钮相关知识感兴趣的朋友一起学习吧

有时候我们需要在游戏或应用中用一些符合我们样式的提示框(alertdialog),以下是我在开发一个小游戏中总结出来的.希望对大家有用.

先上效果图:

Android使用自定义alertdialog实现确认退出按钮

下面是用到的背景图或按钮的图片

经过查找资料和参考了一下例子后才知道,要实现这种效果很简单.就是在设置alertdialog的contentview.

以下的代码是写在activity下的,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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();
// *** 主要就是在这里实现这种效果的.
// 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
window.setcontentview(r.layout.shrew_exit_dialog);
// 为确认按钮添加事件,执行退出应用操作
imagebutton ok = (imagebutton) window.findviewbyid(r.id.btn_ok);
ok.setonclicklistener(new view.onclicklistener() {
public void onclick(view v) {
exitapp(); // 退出应用...
}
});
// 关闭alert对话框架
imagebutton cancel = (imagebutton) window.findviewbyid(r.id.btn_cancel);
cancel.setonclicklistener(new view.onclicklistener() {
public void onclick(view v) {
dlg.cancel();
}
});
}以下的是layout文件,定义了对话框中的背景与按钮.点击事件在activity中添加.
文件名为 : shrew_exit_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<relativelayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<!-- 退出游戏的背景图 -->
<imageview android:id="@+id/exitgamebackground"
android:layout_centerinparent="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/bg_exit_game" />
<!-- 确认按钮 -->
<imagebutton android:layout_alignbottom="@+id/exitgamebackground"
android:layout_alignleft="@+id/exitgamebackground"
android:layout_marginbottom="30dp"
android:layout_marginleft="35dp"
android:id="@+id/btn_ok"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/btn_ok" />
<!-- 取消按钮 -->
<imagebutton android:layout_alignbottom="@+id/exitgamebackground"
android:layout_alignright="@+id/exitgamebackground"
android:layout_marginbottom="30dp"
android:layout_marginright="35dp"
android:id="@+id/btn_cancel"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/btn_cancel" />
</relativelayout>就这样经过了以上几步,就可以实现自定义alertdialog的效果了. 用同样的思路可以实现其它更复杂的效果.

alertdialog实现确认退出按钮实例代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.example.alertdialog;
import android.os.bundle;
import android.app.activity;
import android.app.alertdialog;
import android.content.dialoginterface;
import android.view.menu;
import android.view.view;
import android.widget.toast;
public class mainactivity extends activity {
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
}
//名字如果是onbackpressed,那就是按下手机返回键的效果,参数为空即可。
public void onbackpressed1(view v) {
new alertdialog.builder(this).settitle("确认退出吗?")
.seticon(android.r.drawable.ic_dialog_info)
.setpositivebutton("确定", new dialoginterface.onclicklistener() {
@override
public void onclick(dialoginterface dialog, int which) {
// 点击“确认”后的操作
mainactivity.this.finish();
}
})
.setnegativebutton("返回", new dialoginterface.onclicklistener() {
@override
public void onclick(dialoginterface dialog, int which) {
// 点击“返回”后的操作,这里不设置没有任何操作
toast.maketext(mainactivity.this, "你点击了返回键", toast.length_long).show();
}
}).show();
}
}

 

延伸 · 阅读

精彩推荐