Android dialog disabled automatically

Source: Internet
Author: User

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 }
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////////////////////////////

Sometimes we need to use a prompt box (alertdialog) that matches our style in a game or application)

The following is my summary in developing a small game. I hope it will be useful to you.

First:


The following figure shows the background image or button image used.


After searching for materials and referring to the example below, it is very easy to achieve this effect, that is, setting the contentview of alertdialog.

The following code is written under activity:

Public Boolean onkeydown (INT keycode, keyevent event ){
// If the return key is used, it is directly returned to the desktop.
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 ();
// *** This effect is implemented here.
// Set the content page of the window. The View content is defined in the shrew_exit_dialog.xml file.
Window. setcontentview (R. layout. shrew_exit_dialog );
// Add an event for the confirm button and exit the application
Imagebutton OK = (imagebutton) window. findviewbyid (R. Id. btn_ OK );
OK. setonclicklistener (New View. onclicklistener (){
Public void onclick (view v ){
Exitapp (); // exit the application...
}
});
 
// Close the alert dialog framework
Imagebutton cancel = (imagebutton) window. findviewbyid (R. Id. btn_cancel );
Cancel. setonclicklistener (New View. onclicklistener (){
Public void onclick (view v ){
DLG. Cancel ();
}
});
} The following is the layout file that defines the background and buttons in the dialog box. Click the event to add it to the activity.

File Name: 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">
 
<! -- Background image for exiting the game -->
<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"/>
 
<! -- Confirm button -->
<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"/>
 
<! -- Cancel button -->
<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> after completing the preceding steps, you can customize alertdialog and use the same idea to implement other more complex effects.

This article from the Linux community website (www.linuxidc.com) original link: http://www.linuxidc.com/Linux/2011-10/44787.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.