Android App-Customize dialog box

Source: Internet
Author: User

Now you're talking about a custom dialog box.

Common dialog box has a lot of users to organize the information, dialog box Daquan ah God horse. But in addition to the usual dialog boxes, sometimes you need to define a new dialog box according to your own needs, here's a little bit of a way to customize the dialog box.


1, need a dialog box content XML configuration file Game_dialog.xml, this should not need to explain too much, basic layout just.

There are several picture buttons, so put a few pictures under the drawable, too troublesome, you can also use the button to replace, to see the effect.

It is necessary to note that all the control's ID here I use @id, because ID I have written in the res/values/ids.xml inside, if not to use @+id, but not good management, although I do in the code management this piece does not particularly good, But try my best to make them look clean, clear and concise, and also make some comments, and then come back to see or make changes will be more comfortable.

<?xml version= "1.0"  encoding= "Utf-8"? ><linearlayout xmlns:android= "http// Schemas.android.com/apk/res/android "    android:layout_width=" Match_parent "     android:layout_height= "Match_parent"     android:gravity= "center_vertical| Center_horizontal|center "    android:orientation=" vertical " >     <textview         android:id= "@id/id_dialog_message"         android:layout_width= "Fill_parent"          android:layout_height= "Wrap_content"          Android:gravity= "Center_horizontal"         android:textsize= "20SP"         android:text= "Output Message"          />    <lineArlayout        android:layout_width= "Fill_parent"          android:layout_height= "Wrap_content"          android:gravity= "Center_horizontal"         android:orientation= " Horizontal " >        <ImageButton             android:id= "@id/id_dialog_next"              android:layout_width= "Wrap_content"              android:layout_height= "Wrap_content"              android:layout_weight= "1"              android:background= "#00000000"              android:src= "@drawable/dialog_next"  />        <imagebutton             android:id= "@id/id_dialog_replay"             android:layout_width= "Wrap_content"             android:layout_height= "Wrap_content"             android:layout_weight= "1"              android:background= "#00000000"              android:src= "@drawable/dialog_replay"  />         <ImageButton             android:id= "@id/id_dialog_continue"              android:layOut_width= "Wrap_content"             android:layout _height= "Wrap_content"             android:layout_ weight= "1"             android:background= "# 00000000 "            android:src=" @drawable/dialog _continue " />        <ImageButton             android:id= "@id/id_dialog_quit"              android:layout_width= "Wrap_content"              android:layout_height= "Wrap_content"              android:layout_weight= "1"             &nbSp;android:background= "#00000000"              android:src= "@drawable/dialog_quit"  />    </linearlayout></linearlayout >


Ids.xml as follows:

<?xml version= "1.0" encoding= "Utf-8"?><resources> <item name= "Id_dialog_next" type= "id" ></item > <item name= "id_dialog_replay" type= "id" ></item> <item name= "id_dialog_continue" type= "id "></item> <item name=" Id_dialog_quit "type=" id "></item> <item name=" Id_dialog_messag E "type=" id "></item></resources>

2, in the res/values under the Styles.xml to add the following, define a style named Gamedialog, and then set some item, basically see meaning can understand, which android: Windowbackground value can use a picture, you can also use an XML file configuration, with the XML will be a little more beautiful, with the picture, I am not very likely to look at the figure, do not know how to match the look. The following is also attached to my use of the shape.xml source.

    <style name= "GameDialog"  parent= "@ Android:Theme.Dialog ">        <item name=" Android: Windowframe "> @null </item>        <item name=" android: Windownotitle ">true</item>        <item name=" Android : Windowbackground "> @drawable/shape</item>        <item  name= "Android:windowisfloating" >true</item>        < Item name= "Android:windowistranslucent" >true</item>         <item name= "Android:windowcontentoverlay" > @null </item>         <item name= "Android:backgrounddimenabled" >false</item>     </style> 

Shap.xml as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><shape android:shape= "Rectangle" xmlns:android= "http// Schemas.android.com/apk/res/android "> <!--<solid android:color=" #55222222 "/>--> <solid android: Color= "#e0000000"/> <corners android:radius= "5dp"/> <padding android:left= "6DP" android:top= "6DP" an droid:right= "6DP" android:bottom= "6DP"/></shape>


3, defines a class inherits from dialog and implements the Onclicklistener interface, uses the Setcontentview to load the view, uses the Findviewbyid to obtain the View object instance in the dialog box (I do not know that is right ah?). ), and as we normally use, we can set up listening events for them.

What needs to be said is these two sentences:

This.setcancelable (FALSE);

To be known, is to block out. Press the back button to close the dialog box, sometimes there is a need to make a choice.

This.dismiss ();

This closes the dialog box.


The following is the source code for Gamedialog:

Package com.test.leetledialog;import android.app.dialog;import android.content.context;import  android.view.view;import android.widget.imagebutton;import android.widget.textview;/** *   Customize Message Notification dialog box   *  @author  leetle *  */public class gamedialog  extends dialog implementsandroid.view.view.onclicklistener {private textview gd _message;private imagebutton gd_next;private imagebutton gd_replay;private  Imagebutton gd_continue;private imagebutton gd_quit;public gamedialog (Context context ,  string message)  {super (Context, r.style.gamedialog); Setcontentview (R.layout.game_dialog) ;gd_message =  (TextView)  findviewbyid (r.id.id_dialog_message);gd_next =  (ImageButton)  findviewbyid (R.id.id_dialog_next);gd_continue =  (ImageButton)  findviewbyid (R.id.id_dialog _continue); gd_replay =  (ImageButton)  findviewbyid (r.id.id_dialog_replay);gd_quit =  (ImageButton)   Findviewbyid (r.id.id_dialog_quit);//  set prompt message gd_message.settext (message);//  Set Button listener Gd_ Next.setonclicklistener (This), Gd_quit.setonclicklistener (This), Gd_continue.setonclicklistener (this); gd_ Replay.setonclicklistener (this);//  setting cannot be canceled by the return key this.setcancelable (false);} @Overridepublic  void onclick (View view)  {switch  (View.getid ())  {case  R.id.id_dialog_next: {system.out.println ("Gamedialog next"); This.dismiss (); break;} Case r.id.id_dialog_continue: {system.out.println ("Gamedialog continue"); This.dismiss (); break;} Case r.id.id_dialog_replay: {system.out.println ("Gamedialog replay"); This.dismiss (); break;} Case r.id.id_dialog_quit: {system.out.println ("Gamedialog quit"); This.dismiss (); break;} Default: {}}}}


4. Open a window in mainactivity

package com.test.leetledialog;import android.os.bundle;import android.app.activity;import  android.view.menu;public class mainactivity extends activity {      @Override     protected void oncreate (bundle savedinstancestate)   {        super.oncreate (savedinstancestate);         setcontentview (R.layout.activity_main);         //new a gamedialog        gamedialog gamedialog;         gamedialog = new gamedialog (MainActivity.this, "Look at me, look at me!"); Gamedialog.show ();    }     @Override     public  boolean oncreateoptionsmenu (Menu menu)  {         Getmenuinflater (). Inflate (r.mEnu.main, menu);        return true;     }    }

Oh, run the results like. Say this code can't be folded?

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/57/B5/wKiom1SiuO3B5B0GAAJMj2UX_Rc518.jpg "title=" Screenshot_2014-12-30-22-37-38.png "width=" 480 "height=" 857 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:480px; height:857px; "alt=" wkiom1siuo3b5b0gaajmj2ux_rc518.jpg "/>


Android App-Customize the dialog box to the end!


2014.12.30

Li Yengiang



Android App-Customize dialog box

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.