Android Click a button to pop up a dialog box

Source: Internet
Author: User

The interface is very simple, is a button, click on this button, will pop up a dialog box

<linearlayout 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 "    android:orientation=" vertical "    tools:context=". Mainactivity ">    <Button        android:onclick=" Onclick1 "      & nbsp Android:layout_width= "Wrap_content"         android:layout_height= "wrap_content"         android:text= "@string/bt_1"  />   <Button        Android:o nclick= "Onclick2"         android:layout_width= "wrap_content"         Android: layout_height= "Wrap_content"         android:text= "@string/bt_2"  />   < button        android:onclick= "Onclick3"         android:layout_width= "wrap_content "        android:layout_height=" wrap_content "        android:text=" @ String/bt_3 " />   </LinearLayout>

Next is the Main method

</pre><pre name= "code" class= "java" >package com.neusoft.dialog;import Junit.framework.assert;import Android.net.wifi.wifimanager.multicastlock;import Android.os.bundle;import Android.app.activity;import Android.app.alertdialog;import Android.app.alertdialog.builder;import Android.content.dialoginterface;import Android.content.dialoginterface.onclicklistener;import Android.content.dialoginterface.onmultichoiceclicklistener;import Android.view.menu;import Android.view.menuitem.onmenuitemclicklistener;import Android.view.view;import Android.widget.abslistview.multichoicemodelistener;import Android.widget.toast;public class MainActivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} public void Onclick1 (view view)//This method pops up a dialog box {alertdialog.builder builder=new Builder (this); Builder.seticon ( R.drawable.ic_launcher)///Set icon Builder.settitle ("If you want to succeed must be self-palace");//Set the title of the dialog builder.setmesSage ("Are you sure you want to go from the palace?" Builder.setpositivebutton ("OK", new Onclicklistener () {//This is the set OK button @overridepublic void OnClick ( Dialoginterface arg0, int arg1) {toast.maketext (Mainactivity.this, "self-palace Success", Toast.length_short). Show ();}); Builder.setnegativebutton ("Cancel", new Onclicklistener () {//Cancel button @overridepublic void OnClick (dialoginterface arg0, int ARG1) {Toast.maketext (Mainactivity.this, "cancellation from Palace", Toast.length_short). Show ();});  Alertdialog b=builder.create ();  B.show (); You must show it to see the dialog box, like toast}public void Onclick2 (view view)//Here is a single-selection dialog box {Alertdialog.builder builder=new Builder (this); Builder.seticon (Android. R.drawable.ic_dialog_info) Builder.settitle ("Please select Gender"), Final String []items=new string[]{"male", "female"}; Builder.setsinglechoiceitems (items,-1, New Onclicklistener () {//which refers to the subscript//dialog of the entry selected by the User: The dialog box triggering this method @overridepublic void OnClick (dialoginterface dialog, int which) {Toast.maketext (Mainactivity.this, "you selected:" +items[which], Toast.length_short). Show ();d Ialog.dismiss (); When a user selects a value, the dialog box}); Builder.show ();//This is also a show dialog box, the above can also}public void Onclick3 (view view)//This is a multi-select dialog box {Alertdialog.builder Builder=new Builder (mainactivity.this); Builder.seticon (Android. R.drawable.ic_dialog_info); Builder.settitle ("Please select the person you think is handsome"); final String []itemsid=new string[]{"Xiao Ming", "Xiao Zhi", "Little Dragon", "Xiao Zhao"} ; Final Boolean []checkeditems=new boolean[]{true,true,false,false};// Here's true is the default number of people who have been selected Builder.setmultichoiceitems (Itemsid, Checkeditems,new Onmultichoiceclicklistener () {@ overridepublic void OnClick (dialoginterface dialog, int which, Boolean ischeck) {Checkeditems[which]=ischeck;}}); /Set a OK button Builder.setpositivebutton ("OK", new Onclicklistener () {@Overridepublic void OnClick (Dialoginterface dialog, int whick) {String text= ""; for (int i=0;i<itemsid.length;i++) {text+=checkeditems[i]?itemsid[i]+ ",": "";} Toast.maketext (mainactivity.this, Text, Toast.length_short). Show ();d Ialog.dismiss ();}); Builder.show ();}}

Source

Android Click a button to pop up a 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.