欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android使用自定義alertdialog實(shí)現(xiàn)確認(rèn)退出按鈕

 更新時(shí)間:2016年01月22日 11:38:31   作者:一葉飄舟  
本文通過(guò)實(shí)例代碼給大家詳解Android使用自定義alertdialog實(shí)現(xiàn)確認(rèn)退出按鈕,對(duì)alertdialog退出按鈕相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧

有時(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(); 
}
}

相關(guān)文章

最新評(píng)論