国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程之自定義AlertDialog(退出提示框)用法實例

2020-04-11 10:59:52
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程自定義AlertDialog(退出提示框)用法,分享給大家供大家參考,具體如下:

有時候我們需要在游戲或應用中用一些符合我們樣式的提示框(AlertDialog)

以下是我在開發一個小游戲中總結出來的.希望對大家有用.

先上效果圖:

下面是用到的背景圖或按鈕的圖片

經過查找資料和參考了一下例子后才知道,要實現這種效果很簡單.就是在設置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();    // *** 主要就是在這里實現這種效果的.    // 設置窗口的內容頁面,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的效果了. 用同樣的思路可以實現其它更復雜的效果.

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新泰市| 建瓯市| 乌拉特后旗| 根河市| 桐柏县| 五家渠市| 清远市| 积石山| 民权县| 泰宁县| 神木县| 靖边县| 全南县| 漳平市| 江城| 广西| 民乐县| 浦北县| 社旗县| 安平县| 淅川县| 兴业县| 自治县| 饶阳县| 山西省| 和政县| 水富县| 波密县| 黄浦区| 济源市| 鹤峰县| 商水县| 济阳县| 奉贤区| 阿巴嘎旗| 余庆县| 广平县| 怀集县| 靖远县| 红桥区| 宜宾县|