Android使用自定義alertdialog實(shí)現(xiàn)確認(rèn)退出按鈕
有時(shí)候我們需要在游戲或應(yīng)用中用一些符合我們樣式的提示框(AlertDialog),以下是我在開(kāi)發(fā)一個(gè)小游戲中總結(jié)出來(lái)的.希望對(duì)大家有用.
先上效果圖:
下面是用到的背景圖或按鈕的圖片
經(jīng)過(guò)查找資料和參考了一下例子后才知道,要實(shí)現(xiàn)這種效果很簡(jiǎn)單.就是在設(shè)置alertDialog的contentView.
以下的代碼是寫(xiě)在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)容頁(yè)面,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對(duì)話框架 ImageButton cancel = (ImageButton) window.findViewById(R.id.btn_cancel); cancel.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { dlg.cancel(); } }); }以下的是layout文件,定義了對(duì)話框中的背景與按鈕.點(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)過(guò)了以上幾步,就可以實(shí)現(xiàn)自定義AlertDialog的效果了. 用同樣的思路可以實(shí)現(xiàn)其它更復(fù)雜的效果.
alertdialog實(shí)現(xiàn)確認(rèn)退出按鈕實(shí)例代碼:
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,那就是按下手機(jī)返回鍵的效果,參數(shù)為空即可。 public void onBackPressed1(View v) { new AlertDialog.Builder(this).setTitle("確認(rèn)退出嗎?") .setIcon(android.R.drawable.ic_dialog_info) .setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 點(diǎn)擊“確認(rèn)”后的操作 MainActivity.this.finish(); } }) .setNegativeButton("返回", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 點(diǎn)擊“返回”后的操作,這里不設(shè)置沒(méi)有任何操作 Toast.makeText(MainActivity.this, "你點(diǎn)擊了返回鍵", Toast.LENGTH_LONG).show(); } }).show(); } }
- Android 自定義AlertDialog對(duì)話框樣式
- Android AlertDialog自定義樣式實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話框的方法
- Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
- Android中AlertDialog的六種創(chuàng)建方式
- Android對(duì)話框AlertDialog.Builder使用方法詳解
- Android編程之自定義AlertDialog(退出提示框)用法實(shí)例
- Android中AlertDialog 點(diǎn)擊按鈕后不關(guān)閉對(duì)話框的功能
- ANDROID中自定義對(duì)話框AlertDialog使用示例
- Android中阻止AlertDialog關(guān)閉實(shí)例代碼
- Android編程自定義AlertDialog樣式的方法詳解
相關(guān)文章
Android中不支持動(dòng)態(tài)申請(qǐng)權(quán)限的原因
這篇文章主要介紹了Android中不支持動(dòng)態(tài)申請(qǐng)權(quán)限的原因,本文列舉了幾個(gè)不支持動(dòng)態(tài)申請(qǐng)權(quán)限的原因,需要的朋友可以參考下2015-01-01Android編程開(kāi)發(fā)中ListView的常見(jiàn)用法分析
這篇文章主要介紹了Android編程開(kāi)發(fā)中ListView的常見(jiàn)用法,較為詳細(xì)的分析了ListView的功能及常見(jiàn)使用方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android如何通過(guò)手機(jī)自動(dòng)獲取短信驗(yàn)證碼
注冊(cè)帳號(hào)時(shí),經(jīng)常需要手機(jī)獲取驗(yàn)證碼,Android如何通過(guò)手機(jī)自動(dòng)獲取短信驗(yàn)證碼,下面看看小編給大家分享的一段代碼,感興趣的小伙伴們可以參考一下2016-03-03Android編程實(shí)現(xiàn)wifi掃描及連接的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)wifi掃描及連接的方法,涉及Android網(wǎng)絡(luò)操作掃描、查找、連接、線程等相關(guān)操作技巧,需要的朋友可以參考下2018-02-02Caused by: android.os.NetworkOnMainThreadException錯(cuò)誤解決辦法
這篇文章主要介紹了Caused by: android.os.NetworkOnMainThreadException錯(cuò)誤解決辦法,本文提供了2種解決方法,需要的朋友可以參考下2014-07-07Android跨應(yīng)用啟動(dòng)實(shí)例詳解
這篇文章主要介紹了 Android跨應(yīng)用啟動(dòng)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04Android自定義View的三個(gè)構(gòu)造函數(shù)
這篇文章主要介紹了Android自定義View的三個(gè)構(gòu)造函數(shù),需要的朋友可以參考下2017-06-06