Android實現“退出確認”對話方塊

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   os   ar   sp   

    @Override    public void onBackPressed() {    new AlertDialog.Builder(this).setTitle("確認退出嗎?")        .setIcon(android.R.drawable.ic_dialog_info)        .setPositiveButton("確定", new DialogInterface.OnClickListener() {             @Override            public void onClick(DialogInterface dialog, int which) {            // 點擊“確認”後的操作            MainFragmentActivity.this.finish();             }        })        .setNegativeButton("返回", new DialogInterface.OnClickListener() {             @Override            public void onClick(DialogInterface dialog, int which) {            // 點擊“返回”後的操作,這裡不設定沒有任何操作            }        }).show();    // super.onBackPressed();    }

 

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

經過尋找資料和參考了一下例子後才知道,要實現這種效果很簡單.就是在設定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實現“退出確認”對話方塊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.