Technology Android Dialog Box Collection

Source: Internet
Author: User

 

Dialog Box Collection

Yu Song Momo Original article reproduced, Please Note: Reproduced from Yu Song Momo blog original address: http://blog.csdn.net/xys289187120/article/details/6601613Yusong Momo takes you to check the dialog box in Android


Today, I used my own demo to describe how to use a dialog box in Android.


1. Confirm to cancel dialog box

The dialog box contains two buttons. by calling the setpositivebutton method and the setnegativebutton method, you can set the display content of the button and the listener event of the button.

Use alerdialog to create a dialog box

AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);   

Use builder to set the title button icon in the dialog box

Builder. seticon (R. drawable. Icon); builder. settitle ("are you sure you want to leave? "); Builder. setpositivebutton ("OK", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {// Add the logic showdialog after clicking OK here ("you have selected OK") ;}}); builder. setnegativebutton ("cancel", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {// Add the logic showdialog after clicking OK here ("you have chosen to cancel") ;}}); builder. create (). show ();
This dialog is used to monitor the content information after the actual onclick operation.

private void showDialog(String str) {  w AlertDialog.Builder(MainDialog.this)       .setMessage(str)       .show();  }  

2. Multiple button information boxes

Alertdialog. builder = new alertdialog. builder (maindialog. this); builder. seticon (R. drawable. icon); builder. settitle ("Vote"); builder. setmessage ("what content do you think will attract you? "); Builder. setpositivebutton ("interesting", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {showdialog ("You have chosen interesting") ;}}); builder. setneutralbutton ("thoughtful", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {showdialog ("You have chosen thoughtful") ;}}); builder. setnegativebutton ("strong topic", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {showdialog ("you have selected a strong topic") ;}}); builder. create (). show ();

3. list box

This array is used for list selection.

final String[] mItems = {"item0","item1","itme2","item3","itme4","item5","item6"};  
Alertdialog. builder = new alertdialog. builder (maindialog. this); builder. settitle ("list selection box"); builder. setitems (mitems, new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int which) {// In the pop-up window after clicking it, select the showdiich ("the id you selected is" + which + ", "+ mitems [which]) ;}}); builder. create (). show ();

4. single choice list box

Msinglechoice is used to record the ID in the single choice

int mSingleChoiceID = -1;  

Alertdialog. builder = new alertdialog. builder (maindialog. this); msinglechoiceid =-1; builder. seticon (R. drawable. icon); builder. settitle ("single choice"); builder. setsinglechoiceitems (mitems, 0, new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {msinglechoiceid = whichbutton; showdialog ("the id you selected is" + whichbutton + ", "+ mitems [whichbutton]) ;}}); builder. setpositivebutton ("OK", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {If (msinglechoiceid> 0) {showdialog ("You selected" + msinglechoiceid) ;}}); builder. setnegativebutton ("cancel", new dialoginterface. onclicklistener () {public void onclick (dialoginterface dialog, int whichbutton) {}}); builder. create (). show ();

5. progress bar


Click the progress bar to enable a thread to calculate the read progress. The read progress is assumed to have ended at 100.
When the value of progress is less than 100, it is used to loop through the process ++ and stop the thread only after the reading is complete.
Mprogressdialog = new progressdialog (maindialog. this); mprogressdialog. seticon (R. drawable. icon); mprogressdialog. settitle ("progress bar window"); mprogressdialog. setprogressstyle (progressdialog. style_horizontal); mprogressdialog. setmax (max_progress); mprogressdialog. setbutton ("OK", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {// The logic after clicking is added here}); mprogressdialog. setbutton2 ("cancel", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {// The logic after clicking is added here}); mprogressdialog. show (); New thread (this ). start (); IC void run () {int progress = 0; while (Progress <max_progress) {try {thread. sleep (100); Progress ++; mprogressdialog. incrementprogressby (1);} catch (interruptedexception e) {// todo auto-generated Catch Block E. printstacktrace ();}}

6. Multi-choice list box



Multichoiceid is used to record multiple selected IDDs that exist in the arraylist.
Select and add it to arraylist.
Deselect and remove the arraylist .

ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>();  
Alertdialog. builder = new alertdialog. builder (maindialog. this); multichoiceid. clear (); builder. seticon (R. drawable. icon); builder. settitle ("Multiple Choices"); builder. setmultichoiceitems (mitems, new Boolean [] {false, false}, new dialoginterface. onmultichoiceclicklistener () {public void onclick (dialoginterface dialog, int whichbutton, Boolean ischecked) {If (ischecked) {multichoiceid. add (whichbutton); showdialog ("the id you selected is" + whichbutton + "," + mitems [whichbutton]);} else {multichoiceid. remove (whichbutton) ;}}); builder. setpositivebutton ("OK", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {string STR = ""; int size = multichoiceid. size (); For (INT I = 0; I <size; I ++) {STR + = mitems [multichoiceid. get (I)] + "," ;}showdialog ("You selected" + Str) ;}}); builder. setnegativebutton ("cancel", new dialoginterface. onclicklistener () {public void onclick (dialoginterface dialog, int whichbutton) {}}); builder. create (). show ();

7. custom Layout

I will talk more about custom la S. Why do I need to talk more about it?
In fact, the custom layout is very important in Android development because it allows developers to make their own colorful activities without using the boring system interface.

What are the benefits of custom dialog?

For example, when the development process is too long, we need to introduce a broadcast sent by the system to bring up a dialog. However, the dialog must be based on the activity to present it. If there is no activity, the program will crash. So we can write a custom dialog to define it as an activity.
In this way, after receiving a broadcast that opens dialog, we can directly start the activity program to run normally ~~

This is the benefit of custom dialog.

Note: The following example only indicates that the custom dialog does not write it separately in an activity. You can change it if necessary.

Alertdialog. builder = new alertdialog. builder (maindialog. this); layoutinflater factory = layoutinflater. from (this); final view textentryview = factory. inflate (R. layout. test, null); builder. seticon (R. drawable. icon); builder. settitle ("Custom input box"); builder. setview (textentryview); builder. setpositivebutton ("OK", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {edittext username = (edittext) textentryview. findviewbyid (R. id. etusername); edittext Password = (edittext) textentryview. findviewbyid (R. id. etpassword); showdialog ("name:" + username. gettext (). tostring () + "Password:" + password. gettext (). tostring () ;}}); builder. setnegativebutton ("cancel", new dialoginterface. onclicklistener () {public void onclick (dialoginterface dialog, int whichbutton) {}}); builder. create (). show ();

<Span style = "color: #000000;"> <? 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" Android: Orientation = "horizontal" Android: id = "@ + ID/dialog"> <linearlayout Android: layout_height = "wrap_content" Android: layout_width = "wrap_content" Android: Orientation = "horizontal" Android: id = "@ + ID/dialogname"> <textview Android: layout_height = "wrap_content" Android: layout_width = "wrap_content" Android: Id = "@ + ID/tvusername" Android: TEXT = "name:"/> <edittext Android: layout_height = "wrap_content" Android: layout_width = "wrap_content" Android: Id = "@ + ID/etusername" Android: minwidth = "200dip"/> </linearlayout> <linearlayout Android: layout_height = "wrap_content" Android: layout_width = "wrap_content" Android: Orientation = "horizontal" Android: id = "@ + ID/dialognum" Android: layout_below = "@ + ID/dialogname"> <textview Android: layout_height = "wrap_content" Android: layout_width = "wrap_content" Android: id = "@ + ID/tvpassword" Android: text = "Password:"/> <edittext Android: layout_height = "wrap_content" Android: layout_width = "wrap_content" Android: id = "@ + ID/etpassword" Android: minwidth = "200dip"/> </linearlayout> </relativelayout> </span>

8. Read progress box

Display a progress bar loading in a circle

Mprogressdialog = new progressdialog (this); mprogressdialog. settitle ("read ing"); mprogressdialog. setmessage ("reading... please wait"); mprogressdialog. setindeterminate (true); mprogressdialog. setcancelable (true); mprogressdialog. show ();

Finally, if you still think that I have not written enough details, it doesn't matter if I paste the source code out. You are welcome to discuss and study Yu Song Momo, hoping to make progress together with everyone.

: Http://download.csdn.net/source/3438085

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.