android dialog自动关闭

Not like Toast which is auto closed after 1-2 seconds, Dialog by default is not auto closed and doesn’t have any settings for auto closing.

In case you want your Dialog to auto-close after a time, then you may use a Timer to handle this task. Here a sample:

view source
print ?
01 import java.util.Timer;
02 import java.util.TimerTask;
03   
04 import android.app.Activity;
05 import android.app.AlertDialog;
06 import android.content.DialogInterface;
07 import android.os.Bundle;
08 import android.os.Handler;
09 import android.os.Message;
10 import android.view.View;
11 import android.widget.Button;
12   
13 public class AlertDialogStudy extends Activity {
14     
15     @Override
16     public void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.main);
19   
20         // get button
21         Button btnShow = (Button)findViewById(R.id.btn_show);
22         btnShow.setOnClickListener(new View.OnClickListener() {
23   
24             @Override
25             public void onClick(View v) {
26                 AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
27                 builder.setTitle("Auto-closing Dialog");
28                 builder.setMessage("After 2 second, this dialog will be closed automatically!");
29                 builder.setCancelable(true);
30   
31                 final AlertDialog dlg = builder.create();
32   
33                 dlg.show();
34   
35                 final Timer t = new Timer();
36                 t.schedule(new TimerTask() {
37                     public void run() {
38                         dlg.dismiss(); // when the task active then close the dialog
39                         t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
40                     }
41                 }, 2000); // after 2 second (or 2000 miliseconds), the task will be active.
42   
43             }
44         });
45     }
46 }
 
 
 
 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
 

有时候我们需要在游戏或应用中用一些符合我们样式的提示框(AlertDialog)

以下是我在开发一个小游戏中总结出来的.希望对大家有用.

先上效果图:

android dialog自动关闭_第1张图片
下面是用到的背景图或按钮的图片

android dialog自动关闭_第2张图片
经过查找资料和参考了一下例子后才知道,要实现这种效果很简单.就是在设置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的效果了. 用同样的思路可以实现其它更复杂的效果.

本篇文章来源于 Linux公社网站(www.linuxidc.com)  原文链接:http://www.linuxidc.com/Linux/2011-10/44787.htm

你可能感兴趣的:(android dialog自动关闭)