Android編程之自定義AlertDialog(退出提示框)用法實(shí)例
本文實(shí)例講述了Android編程自定義AlertDialog(退出提示框)用法,分享給大家供大家參考,具體如下:
有時(shí)候我們需要在游戲或應(yīng)用中用一些符合我們樣式的提示框(AlertDialog)
以下是我在開發(fā)一個(gè)小游戲中總結(jié)出來的.希望對大家有用.
先上效果圖:

下面是用到的背景圖或按鈕的圖片
經(jīng)過查找資料和參考了一下例子后才知道,要實(shí)現(xiàn)這種效果很簡單.就是在設(shè)置alertDialog的contentView.
以下的代碼是寫在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();
// *** 主要就是在這里實(shí)現(xiàn)這種效果的.
// 設(shè)置窗口的內(nèi)容頁面,shrew_exit_dialog.xml文件中定義view內(nèi)容
window.setContentView(R.layout.shrew_exit_dialog);
// 為確認(rèn)按鈕添加事件,執(zhí)行退出應(yīng)用操作
ImageButton ok = (ImageButton) window.findViewById(R.id.btn_ok);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
exitApp(); // 退出應(yīng)用...
}
});
// 關(guān)閉alert對話框架
ImageButton cancel = (ImageButton) window.findViewById(R.id.btn_cancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dlg.cancel();
}
});
}
以下的是layout文件,定義了對話框中的背景與按鈕.點(diǎn)擊事件在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" /> <!-- 確認(rèn)按鈕 --> <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>
就這樣經(jīng)過了以上幾步,就可以實(shí)現(xiàn)自定義AlertDialog的效果了. 用同樣的思路可以實(shí)現(xiàn)其它更復(fù)雜的效果.
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- android 彈出提示框的使用(圖文實(shí)例)
- Android使用Toast顯示消息提示框
- android實(shí)現(xiàn)彈出提示框
- Android仿QQ、微信聊天界面長按提示框效果
- Android仿IOS自定義AlertDialog提示框
- Android仿百度谷歌搜索自動(dòng)提示框AutoCompleteTextView簡單應(yīng)用示例
- Android超實(shí)用的Toast提示框優(yōu)化分享
- Android中仿IOS提示框的實(shí)現(xiàn)方法
- Android模擬美團(tuán)客戶端進(jìn)度提示框
- Android模仿Toast實(shí)現(xiàn)提示框效果
相關(guān)文章
Android RadioGroup和RadioButton控件簡單用法示例
這篇文章主要介紹了Android RadioGroup和RadioButton控件簡單用法,結(jié)合實(shí)例形式分析了Android單選按鈕控件的基本定義、布局與功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-07-07
Android 聽筒模式的具體實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了Android 聽筒模式的具體實(shí)現(xiàn)實(shí)例,有需要的朋友可以參考一下2013-12-12
Android基于反射技術(shù)實(shí)現(xiàn)的加減乘除運(yùn)算示例
這篇文章主要介紹了Android基于反射技術(shù)實(shí)現(xiàn)的加減乘除運(yùn)算,較為詳細(xì)的描述了反射技術(shù)的原理,并結(jié)合完整實(shí)例形式分析了Android基于反射技術(shù)實(shí)現(xiàn)加減乘除四則運(yùn)算的相關(guān)操作步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10
android實(shí)現(xiàn)左右側(cè)滑菜單效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)左右側(cè)滑菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
android通過led實(shí)現(xiàn)手電筒功能
這篇文章主要為大家詳細(xì)介紹了android通過led實(shí)現(xiàn)手電筒功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
百度地圖API提示230 錯(cuò)誤app scode碼校驗(yàn)失敗的解決辦法
筆者近2天在 Android Studio上玩了一下百度地圖,碰到了常見的"230錯(cuò)誤 APP Scode校驗(yàn)失敗",下面我來介紹一下具體的解決辦法2016-01-01
使用Compose制作抖音快手視頻進(jìn)度條Loading動(dòng)畫效果
這篇文章主要為大家介紹了使用Compose制作抖音快手視頻進(jìn)度條Loading動(dòng)畫效果,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08

