A simple Android example: AlertDialog and androidalertdialog
I recently learned about Android for a while and found some small demos on the Internet. I have simulated the Demo myself. First of all, I would like to thank my friends who provided the example.
Today we are mainly studying the use of simple Dialog (Implementation exit Dialog box) and custom Dialog box.
1. Exit dialog box
The dialog box in normal mode is easy to use. It mainly includes setting the dialog box title, setting the dialog box content, setting the buttons in the dialog box, and adding listening events. The main code is as follows:
// Normal Style dialog box btn2 = (Button) findViewById (R. id. btn2); btn2.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub/** declare a Builder object in the current Activity **/AlertDialog. builder ad = new Builder (MainActivity. this);/** set the title **/ad. setTitle ("exit application");/** set topic content **/ad. setMessage ("exit application? ");/** Set button **/ad. setPositiveButton ("OK", new DialogInterface. onClickListener () {/** button to increase the listening time and close activity **/public void onClick (DialogInterface dialog, int which) {MainActivity. this. finish (); System. exit (0) ;}});/** set button **/ad. setNegativeButton ("cancel", new DialogInterface. onClickListener () {/** sets the listening cancellation time and closes the current dilaog **/public void onClick (DialogInterface dialog, int which) {dialog. dismiss () ;}}); ad. create (). show ();}});
2. Custom dialog box
User-defined dialog boxes are relatively complicated to use. They mainly set styles for custom dialog boxes. When learning this content, they learned how to use selector.
Selector in Android is mainly used to change the default background of ListView and Button controls.
For example, the two button styles defined in this demo
1.
<? Xml version = "1.0" encoding = "UTF-8"?> <Selector xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- 1. android: state_focused: when the control gets Focus 2. android: state_pressed: when the control is pressed 3. android: state_selected: when the control is selected --> <item android: drawable = "@ drawable/linkbtnbgedim" android: state_pressed = "true"/> <item android: drawable = "@ drawable/linkbtnbg"/> </selector>
2.
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/imgcanceled" android:state_pressed="true"/> <item android:drawable="@drawable/imgcancel"/></selector>
Style code of the custom dialog box
<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <! -- Use relative layout on the top to make it easier to do --> <RelativeLayout android: id = "@ + id/customviewlayTitle" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: background = "#1A94F9"> <TextView android: id = "@ + id/customviewtvTitle" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerVertical = "true" android: padding = "10dp" android: text = "About Us" android: textColor = "#0 00000 "/> <ImageButton android: id =" @ + id/customviewtvimgCancel "android: layout_width =" 40dp "android: layout_height =" 40dp "android: layout_alignParentRight =" true "android: layout_centerVertical = "true" android: background = "@ drawable/canceltor"/> </RelativeLayout> <! -- Intermediate content --> <LinearLayout android: id = "@ + id/customviewlayMessage" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: orientation = "vertical" android: layout_below = "@ id/customviewlayTitle" android: padding = "20dp"> <TextView android: id = "@ + id/orthertv0" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Author: maozhanlei" android: textColor = "#0 00000 "/> <TextView android: id =" @ + id/orthertv1 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: linksClickable =" true "android: text = "blog: http://www.cnblogs.com/qadada/" android: textColor = "#000000"/> <TextView android: id = "@ + id/orthertv2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: linksClickable = "true" android: text = "Email: mzl0819 @ Gmail.com "android: textColor =" #000000 "/> </LinearLayout> <! -- Bottom button --> <LinearLayout android: id = "@ + id/customviewlayLink" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_below = "@ id/customviewlayMessage" android: orientation = "horizontal" android: paddingLeft = "20dp" android: paddingRight = "20dp" android: paddingBottom = "20dp"> <Button android: id = "@ + id/ortherbtnemil" android: layout_width = "fill_parent" android: layout_height = "40dp" android: background = "@ drawable/linkbtnbged" android: linksClickable = "true" android: layout_weight = "1" android: layout_marginRight = "10dp" android: text = "Email to author"/> <Button android: id = "@ + id/ortherbtnweb" android: layout_width = "fill_parent" android: layout_height = "40dp" android: linksClickable = "true" android: background = "@ drawable/linkbtnbged" android: text = "Access blog" android: layout_marginLeft = "10dp" android: layout_weight = "1"/> </LinearLayout> </RelativeLayout>
Core java code
/*** Click the listener button on the main interface **/private void onClickBtnListener () {Builder builder = myBuilder (MainActivity. this);/** call the show method of Build to display, and return an AlertDialog object **/final AlertDialog dialog = builder. show ();/** click whether to display the dialog box on the outside of the screen **/dialog. setCanceledOnTouchOutside (false);/** click the button on the right to close the dialog box **/imageCloseDialog (dialog); sendEmailClickListener (dialog); openBlogListener (dialog );} /*** access blog listening event * @ param dialog */private Void openBlogListener (final AlertDialog dialog) {Button ortherbtnweb = (Button) customView. findViewById (R. id. ortherbtnweb); ortherbtnweb. setOnClickListener (new OnClickListener () {/* click the access blog button to visit my blog website * @ see android. view. view. onClickListener # onClick (android. view. view) */@ Override public void onClick (View v) {// TODO Auto-generated method stub Uri = uri. parse ("http://blog.csdn.net/asinzuo "); Intent intent = new Intent (Intent. ACTION_VIEW, uri); startActivity (intent); dialog. dismiss () ;}}) ;}/ *** send email Button event * @ param dialog */private void sendEmailClickListener (final AlertDialog dialog) {Button ortherbtnemil = (Button) customView. findViewById (R. id. ortherbtnemil); ortherbtnemil. setOnClickListener (new OnClickListener () {/* start the mail sending service * @ see android. view. view. onClickListener # onClick (Android Oid. view. view) * // @ Override public void onClick (View v) {// TODO Auto-generated method stub Intent I = new Intent (Intent. ACTION_SEND); I. setType ("text/plain"); I. setType ("message/rfc822"); // use this line on the real machine I. putExtra (Intent. EXTRA_EMAIL, new String [] {"asinzuo@qq.com"}); I. putExtra (Intent. EXTRA_SUBJECT, "windows Style dialog feedback"); I. putExtra (Intent. EXTRA_TEXT, "content"); startActivity (Intent. createChooser (I, "select Application" ); Dialog. dismiss () ;}}) ;}/ *** click Close image to close the pop-up box * @ param dialog */private void imageCloseDialog (final AlertDialog dialog) {/** get the close button from customView. The returned value is an ImageButton **/ImageButton customviewtvimgCancel = (ImageButton) customView. findViewById (R. id. customviewtvimgCancel);/** sets a listener event for ImageButton and implements the **/customviewtvimgCancel using an anonymous internal class. setOnClickListener (new OnClickListener () {public void onClick (View v ){/ ** Call the dismiss method of AlertDiloag to disable **/dialog. dismiss () ;}}) ;}/ *** generates a Builder object * @ param mainActivity * @ return */private Builder myBuilder (MainActivity mainActivity) {// TODO Auto-generated method stub/*** it is similar to findViewById (). The difference is that LayoutInflater is used to find the xml layout file under res/layout/and instantiate it; * While findViewById () is to find the specific widget control in the xml layout file ** // ** get a LayoutInflater object from the specified activity **/final LayoutInflater inflater = this. getLayoutInflater ();/** inflate () is used to find a layout file defined in xml. The returned value is the view type. **/customView = inflater. inflate (R. layout. windowsdialog, null);/** create a Builder object **/AlertDialog. builder builder = new AlertDialog. builder (mainActivity); return builder. setView (customView );}
Download Demo