Android custom dialog box alertdialog

Source: Internet
Author: User

Some time ago, when designing the UI, we saw that the pop-up menu of UC was well designed:

As a result, I wrote this effect on myself,
The main code is as follows:
DLG = new alertdialog. Builder (context). Create ();
DLG. Show ();
DLG. getwindow (). setcontentview (R. layout. alert_style );
Layoutinflater factory = layoutinflater. From (context );
View view = factory. Inflate (R. layout. alert_style, null );
GV = (gridview) view. findviewbyid (R. Id. mygrid );
GV. setadapter (New imageadapter (context, new integer [] {R. drawable. menu_mark_editor, R. drawable. menu_delete }));
DLG. getwindow (). setcontentview (GV );
In fact, the principle is very simple. In the pop-up box alertdialog, add a view to him. Here I am using a row of pictures displayed in the gridview. The effect is similar to that of UC !! You can also use listview instead of the gridview. The principle is the same!

 

 


Before starting this chapter, I would like to thank the authors of several articles. This article is based on the efforts of our predecessors to find a method to completely reload Android alertdialog.

1. alertdialog basic usage: http://wenku.baidu.com/view/2054551910a6f524ccbf85ba.html

After reading this article, we first felt that the method for rewriting alertdialog was setview (view)

You will soon find three problems:

1) Once setmessage and settitle are set, you cannot achieve the expected effect.

Cause: Check the android source code. When alertdialog uses the XML resource file alert_dialog.xml, the file is located in framework/base/CORE/RES/value.

You can also use sourceinsight to search. Open the file. alertdialog stores the view passed by setview under the view where the message is located, rather than overwriting the original layout.

Solution: setmessage and settitle cannot be set. The information required by the dialog box is unmounted from the custom view. Of course, this method still cannot solve the theme and style settings of alertdialog. It only solves the layout problem.

2) There is no button, that is, the corresponding response function of setpositivebutton cannot be set; otherwise, the layout is invalid. This is a terrible point.

3) As mentioned above, style setting cannot be solved, that is, texture cannot be created.

No ........ (Depressed ....)

2. alertdialog advanced http://wenku.baidu.com/view/2054551910a6f524ccbf85ba.html

The author obtains the view instance through reflection technology and obtains the subcontrol instance through findviewbyid, so as to set the message and title, and even can add the button and Response Function by himself. Solve the above problems 1 and 2. But there are still two problems,

1) The Edge white box cannot be removed, and the custom is not fully implemented.

2) if such alertdialog is added with the response of the button, it will turn it into a blocking type, and the UI will be stuck, because they all do things in the main UI thread.

Continue to deny it ...... (Mania ....)

3. alertdialog advanced http://crazier9527.javaeye.com/blog/729034 and http://dev.10086.cn/blog/index.php? Uid-634946-action-viewspace-itemid-4494

The author uses setcontentview to implement complete layout and resource customization, with high flexibility. But there are still problems.

1) poor scalability. The code is concentrated in a class, which is not conducive to maintenance. The reader thinks about the reason on his own.

2) if such alertdialog is added with the response of the button, it will turn it into a blocking type, and the UI will be stuck, because they all do things in the main UI thread.

Deny it again ....... (I have a sea of trouble .... Greetings to the 18 generations of Android)

4. alertdialog proficient http://www.blogjava.net/nokiaguy/archive/2010/07/27/327270.html

When I read this article, I will know that the author is not a general generation. The source code of this article is indeed in-depth. Although it does not solve our problems, it can help us develop ideas.

This is what I want. It is a multi-thread, non-blocking method to rewrite the Response Behavior of the alertdialog button. Good text ....

 

 

After reading the above articles, I will sort out my ideas and come up with the final solution:

1. To fully customize the layout resources, the APIS provided by Android are not enough (I think there was no such concern for WPF at the beginning)

2. Android has no API to customize the button action.

3. to implement non-blocking alertdialog, Android does not provide a framework.

4. You need to use the overload and reflection technologies to obtain the access permission for the private variables of alertdialog. Android has no reference materials.

 

The above are both difficult points and ideas.

The method is as follows:

1. Use reflection technology to dynamically obtain the view resources

Second, you can use custom la s to flexibly set resources and styles and dynamically load them. Getwindow. setcontentview implementation

Third, load alertdialog to load your own theme. If you do not understand it, you can refer to the private constructor of alertdialog: The one with two parameters, which is included in the source code.

4. Reload common functions, or the required functions, and implement parametric settings of controls.

Fifth, the implementation method of the imitation android to achieve multi-resolution support for layout files

Sixth, the android implementation method is counterfeited to implement multi-thread Implementation of Button response.

 

Core code:

Public class myalertdialog extends alertdialog {

Private Final Static string tag = "myalertdialog ";
 
Protected myalertdialog (context ){
Super (context );
}

/**
* @ Param Context
* @ Param theme
*/
Protected myalertdialog (context, int theme ){
Super (context, theme );
}

 

@ Override
Public Boolean onkeydown (INT keycode, keyevent event ){
Return super. onkeydown (keycode, event );
}

@ Override
Public Boolean onkeyup (INT keycode, keyevent event ){
Return super. onkeydown (keycode, event );
}

 

Public static class builder extends alertdialog. Builder {

 

@ Override
Public alertdialog show (){
Log. D (TAG, "show ");
This. Create ();
Dialog. Show ();
Installcontent ();
Return dialog;
}

Private void installcontent (){
Log. D (TAG, "installcontent ");
Mdialoginterface = (dialoginterface) dialog;
If (title! = NULL)
(Textview) view. findviewbyid (R. Id. Title). settext (this. Title );
If (title! = NULL)
(Textview) view. findviewbyid (R. Id. Message). settext (this. Message );
If (iconid! =-1)
(Imageview) view. findviewbyid (R. Id. Icon). setimageresource (iconid );
If (view! = NULL)
Dialog. getwindow (). setcontentview (View );

Mbuttonpositive = (button) view. findviewbyid (R. Id. button1 );
Mbuttonpositive. setonclicklistener (mbuttonhandler );
Mbuttonpositive. settext (mbuttonpositivetext );

Mbuttonnegative = (button) view. findviewbyid (R. Id. button2 );
Mbuttonnegative. setonclicklistener (mbuttonhandler );
Mbuttonnegative. settext (mbuttonnegativetext );
}

Public void setbutton (INT whichbutton, charsequence text,
Dialoginterface. onclicklistener listener, message MSG ){

If (MSG = NULL & listener! = NULL ){
MSG = mhandler. obtainmessage (whichbutton, listener );
}

Switch (whichbutton ){

Case dialoginterface. button_positive:
Mbuttonpositivetext = text;
Mbuttonpositivemessage = MSG;
Break;

Case dialoginterface. button_negative:
Mbuttonnegativetext = text;
Mbuttonnegativemessage = MSG;
Break;

// Case dialoginterface. button_neutral:
// Mbuttonneutraltext = text;
// Mbuttonneutralmessage = MSG;
// Break;

Default:
Throw new illegalargumentexception ("button does not exist ");
}
}

View. onclicklistener mbuttonhandler = new view. onclicklistener (){
Public void onclick (view v ){
Message M = NULL;
If (V = mbuttonpositive & mbuttonpositivemessage! = NULL ){
M = message. Obtain (mbuttonpositivemessage );
} Else if (V = mbuttonnegative
& Mbuttonnegativemessage! = NULL ){
M = message. Obtain (mbuttonnegativemessage );
}
// Else if (V = mbuttonneutral & mbuttonneutralmessage! = NULL ){
// M = message. Obtain (mbuttonneutralmessage );
//}
If (M! = NULL ){
M. sendtotarget ();
}

// Post a message so we dismiss after the above handlers are
// Executed
Mhandler. obtainmessage (buttonhandler. msg_dismiss_dialog,
Mdialoginterface). sendtotarget ();
}
};

}
 
Public static final class buttonhandler extends handler {
// Button clicks have message. What as the button {1, 2, 3} constant
Private Static final int msg_dismiss_dialog = 1;

Private weakreference <dialoginterface> mdialog;

Public buttonhandler (dialoginterface DIALOG ){
Mdialog = new weakreference <dialoginterface> (DIALOG );
}

@ Override
Public void handlemessage (Message MSG ){
Switch (msg. What ){

Case dialoginterface. button_positive:
Case dialoginterface. button_negative:
Case dialoginterface. button_neutral:
(Dialoginterface. onclicklistener) msg. OBJ). onclick (
Mdialog. Get (), MSG. What );
Break;

Case msg_dismiss_dialog:
(Dialoginterface) msg. OBJ). Dismiss ();
}
}
}

 

Call:

Layoutinflater factory = layoutinflater. From (context );
View view = factory. Inflate (R. layout. alertdialoglayout, null );

New myalertdialog. Builder (context)
. Settitle (getstring (R. String. Warning ))
. Setmessage (Message)
. Setview (view)
. Seticon (R. drawable. dialog_icon)
. Setpositivebutton (getstring (R. String. Yes), new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface, int I ){
............

}
})
. Setnegativebutton (getstring (R. String. no), new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int which ){
............

}
})
. Show ();

 

Related Article

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.