The use of Android dialog

Source: Internet
Author: User

A dialog box is a small form that prompts the user to make a decision or enter additional information. The dialog box does not fill the screen and is typically used for modal events that require the user to take certain actions before the program can continue execution. The following is used in the program to specify the use of dialog

Content in Activity_main.xml:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"      xmlns:tools= "Http://schemas.android.com/tools"     android:layout_width= "Match_parent"     android:layout_height= "Match_parent" >    <Button         android:id= "@+id/button1"          Android:layout_width= "wrap_content"         android:layout_height= "wrap _content "        android:layout_alignparentleft=" true "         android:layout_alignparenttop= "true"          android:onclick= "ShowDialog"         android:text= "Normal Dialog"  />    <button        android:id= "@+id/ Button2 "&NBSP;&NBSP;&NBSP;&NBsp;    android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"         android:layout_ Alignparentleft= "true"         android:layout_below= "@+id/button1"         android:onclick= "ShowDialog"          android:text= "dialog box with buttons"  />    <Button         android:id= "@+id/button3"         android: Layout_width= "Wrap_content"         android:layout_height= "wrap_content "        android:layout_alignparentleft=" true "         android:layout_below= "@+id/button2"          android:onclick= "ShowDialog"         android:text= "List dialog box"  />    <Button         android:id= "@+id/button4"          android:layout_width= "Wrap_content"         android:layout_height = "Wrap_content"         android:layout_alignparentleft= "true"          android:layout_below= "@+id/button3"           android:onclick= "ShowDialog"         android: text= "Radio dialog box"  />    <Button         Android:id= "@+id/button5"         android:layout_width= "Wrap_content"         android:layout_height= "Wrap_content"          androiD:layout_alignparentleft= "true"         android:layout_below= "@+id/ Button4 "        android:onclick=" ShowDialog "         android:text= "Multi-Select dialog box"  />    <Button         android:id= "@+id/button6"         android : layout_width= "wrap_content"         android:layout_height= "Wrap_ Content "        android:layout_alignparentleft=" true "         android:layout_below= "@+id/button5"          android:onclick= "ShowDialog"         android:text= "Customize dialog box"  />    <button        android:id= "@+id/ Button7 "&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;   android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"         android:layout_ Alignparentleft= "true"         android:layout_below= "@+id/button6"         android:onclick= "ShowDialog"          android:text= "Timepickdialog dialog box"  />    <Button         android:id= "@+id/button8"          Android:layout_width= "wrap_content"         android:layout_height= "wrap _content "        android:layout_alignparentleft=" true "         android:layout_below= "@+id/button7"          android:onclick= "ShowDialog"         android:text= "Datepickerdialog dialog box"  /></RelativeLayout> 

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/5D/B8/wKioL1UjzmvCA0_YAAEb7IsBxCc007.jpg "title=" 2015-04-07_202628.png "alt=" Wkiol1ujzmvca0_yaaeb7isbxcc007.jpg "/>

Create a new Dialog_button_item.xml layout file:

<?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:orientation= "Horizontal"   >    <button        android:id= "@+id/button1 "        android:layout_width=" 0DP "         android:layout_weight= "1"         android:layout_ height= "wrap_content"         android:text= "button"  />     <button        android:id= "@+id/button2"         android:layout_width= "0DP"          android:layout_weight= "1"         android:layout_height= "Wrap_content"          android:text= "button"  />    <Button         android:id= "@+id/button3"         android:layout_ Width= "0DP"         android:layout_weight= "1"          android:layout_height= "Wrap_content"          android:text= "button"  />    <Button         android:id= "@+id/button4"         android:layout_width= "0DP"         android:layout_weight= "1"          android:layout_height= "Wrap_content"         android:text= " Button " /></linearlayoUt> 

Content in Mainactivity.java:

package com.zzh.day11_dialog;import java.util.calendar;import android.app.activity;import  android.app.alertdialog;import android.app.datepickerdialog;import  android.app.datepickerdialog.ondatesetlistener;import android.app.timepickerdialog;import  android.content.dialoginterface;import android.content.dialoginterface.onclicklistener;import  android.content.dialoginterface.onmultichoiceclicklistener;import android.os.bundle;import  android.view.view;import android.widget.button;import android.widget.datepicker;import  Android.widget.timepicker;import android.widget.toast;public class mainactivity extends  activity{@Overrideprotected  void oncreate (bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main);} /** *  @param  view */public void showdialog (view view) {switch  ( View.getid ()) {Case r.id.button1://General dialog box Alertdialog.builder builder = new alertdialog.builder (this); Builder.setTitle (" General dialog box ");//Settings dialog box title Builder.seticon (r.drawable.ic_launcher);//Settings dialog box small icon builder.setmessage (" Information Information Information ");// Set the dialog box to display the information alertdialog ad = builder.create ();//Create Alertdialog Object Ad.show ();//The Settings dialog box displays Break;case  r.id.button2://a dialog box with a button/** * setpositivebutton,setnegativebutton,setneutralbutton the meaning of the parameter  *   Parameter one: the button to display the text  *  parameter two: button click event  *  */new alertdialog.builder (This). Settitle (" Button "). SetIcon (R.drawable.ic_launcher)//.setmessage (" dialog box with Buttons ")//.setpositivebutton (" OK ", new  Onclicklistener ()//To the OK button event monitoring, processing. The Setnegativebutton,setneutralbutton behind it is the same. {@Overridepublic  void onclick (Dialoginterface dialog, int which) {Toast.maketext ( mainactivity.this,  "You clicked OK button",  toast.length_short). Show ();}}) . Setnegativebutton ("Cencel",  null)//.setneutralbutton ("So so",  null). Show (); Break;caThe contents of the se r.id.button3://dialog box are a list of New alertdialog.builder (this). Settitle ("list"). SetIcon (R.drawable.ic_ Launcher)//.setitems (new string[]{"Entry One", "Entry II", "entry three", "Entry Four"}, new onclicklistener () {/* *   dialoginterface dialog: Clicked Object  * int which: Clicked position  */@Overridepublic   Void onclick (Dialoginterface dialog, int which) {Toast.maketext (MainActivity.this,  which+ "----",  toast.length_short). Show ();}}). Show (); break;case r.id.button4://the contents of the dialog box is a single-selection list/** * setsinglechoiceitems in the meaning of the parameter  *  Parameter one: What are the entries in the Radio box, an array  *  parameter two: The default selected Radio box  *  parameter three: Click event  *  Overloaded Method: *  Setsinglechoiceitems (int itemsid, int checkeditem, dialoginterface.onclicklistener  Listener)  *  parameter one: the array is defined in the XML file, in the Java file using R.array .... Reference.  * */new alertdialog.builder (This). Settitle ("Radio"). SetIcon (R.drawable.ic_launcher)//. Setsinglechoiceitems (new string[]{"Red", "Blue "," Green "}, -1, new onclicklistener () {@Overridepublic  void onclick ( Dialoginterface dialog, int which) {Toast.maketext (mainactivity.this,  "Selected   " + ( which+1) + "  colors",  toast.length_short). Show ();}}). Show (); break;case r.id.button5://Multi-select dialog box/** * setmultichoiceitems (Charsequence[] items,  boolean[] checkeditems, dialoginterface.onmultichoiceclicklistener listener)  *  Parameter one: the entry in the multi-box; *  parameter two: Whether it is selected, if it is null, there is no check  *  parameter three: Unlike the event of a radio box, Dialoginterface.onmultichoiceclicklistener * */new alertdialog.builder (This). SetTitle ("Multi-select"). SetIcon (R.drawable.ic_launcher)//.setmultichoiceitems (new string[]{"Red", "Blue", "green"}, null,  new onmultichoiceclicklistener () {/* onclick (Dialoginterface dialog, int which,  boolean ischecked)  *  parameter one: clicked on the object; *  parameter two: the location is clicked, the position is starting from 0  *  Parameter three: whether the clicked object is selected or True if checked, asfalse */@Overridepublic  void onclick (Dialoginterface dialog, int which,  boolean ischecked) {Toast.maketext (mainactivity.this, ) selected    + (which+1) +   colors,  toast.length_short). Show ();}}). Show (); break;case r.id.button6://custom dialog Box View view_button = getlayoutinflater (). Inflate ( R.layout.dialog_button_item, null);//convert template to convert a well-defined button small XML file into a View object final alertdialog dialog =  new alertdialog.builder (This). Settitle ("Custom"). SetIcon (R.drawable.ic_launcher). Setview (View_button). Create ();//dialog.show (); button btn =  (Button)  view_button.findviewbyid (r.id.button1);// Set the Click event for the button Btn.setonclicklistener (New view.onclicklistener ()  {@Overridepublic  void onclick ( VIEW&NBSP;V)  {toast.maketext (mainactivity.this,  "Lalalla",  toast.length_short). Show (); Dialog.dismiss ();//After the event is completed, the dialog disappears}); break;case r.id.button7://Time Selection dialog box, hours/** * timepiCkerdialog (context context, timepickerdialog.ontimesetlistener callback, int  Hourofday, int minute, boolean is24hourview)  *  parameter one: Context  *  Parameter two: Timepickerdialog's Click event, *  parameter three: hour  *  parameter four: minutes  *  parameter five: whether to use 24-hour system, is true * * /timepickerdialog tpd = new timepickerdialog (this,new  Timepickerdialog.ontimesetlistener ()//The interface for this listener event is in Timepickerdialog.ontimesetlistener () {@Overridepublic  void  ontimeset (Timepicker view, int hourofday, int minute) {Toast.makeText ( mainactivity.this,  " " +hourofday+ ":" +minute, toast.length_short). Show ();}},1,1,true ); Tpd.show (); break;case r.id.button8://Date selection dialog box, month and day/** * datepickerdialog (Context context,  DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear,  Int dayofmonth)  *  parameter one: context  *  parameter two: Datepickerdialog Click event, &Nbsp;*  parameter three: Set the year  *  parameter four: Set the month  *  parameter five: set the day  */datepickerdialog dpd = new  datepickerdialog (This, new ondatesetlistener () {@Overridepublic  void ondateset ( Datepicker view, int year, int monthofyear,int dayofmonth) {Toast.makeText ( mainactivity.this,  " " +year+ " : " + (monthofyear+1) + " : " +dayofmonth,  Toast.length_short). Show ();}}, calendar.getinstance (). Get (Calendar.year),  calendar.getinstance (). Get (Calendar.month),  calendar.getinstance (). Get (Calendar.day_of_month));DPD. Show (); break;}}}

Examples of effects shown:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5D/B8/wKioL1UjzyXCPLNQAAEdv3sv0Ho647.jpg "style=" float: Left; "title=" 2015-04-07_202954.png "alt=" wkiol1ujzyxcplnqaaedv3sv0ho647.jpg "/>650" this.width=650; "src=" http ://s3.51cto.com/wyfs02/m01/5d/b8/wkiol1ujz1_zxbafaaex8-bvxca003.jpg "style=" float:left; "title=" 2015-04-07_ 203057.png "alt=" Wkiol1ujz1_zxbafaaex8-bvxca003.jpg "/>












Interested, you can use the program to study slowly.

This article is from the "Meu Late injury" blog, please be sure to keep this source http://zzhhz.blog.51cto.com/7107920/1629801

The use of Android dialog

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.