Android Learning Note (vii)--Show dialog window

Source: Internet
Author: User

Display the dialog box window


1. Create the DIALOG1 project and add a button to the Activity_main.xml file:

<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 "    android:paddingbottom= "@dimen/activity_vertical_margin"    android:paddingleft= "@dimen/activity_ Horizontal_margin "    android:paddingright=" @dimen/activity_horizontal_margin "    android:paddingtop=" @dimen /activity_vertical_margin "    tools:context=". Mainactivity ">    <button        android:id=" @+id/btn_dialog "        android:layout_width=" Fill_parent "        android:layout_height= "wrap_content"        android:onclick= "OnClick"        android:text= "click to display a Dialog "/></relativelayout>

2. Implement the Create dialog in the Mainactivity.java file and add the following code:

Package Com.example.dialog;import Android.os.bundle;import Android.app.activity;import android.app.AlertDialog; Import Android.app.dialog;import android.content.dialoginterface;import Android.view.menu;import Android.view.View Import Android.widget.toast;public class Mainactivity extends Activity {charsequence[] items = {"Google", "Apple", "MICR Osoft "};boolean[] itemschecked = new Boolean[items.length]; @Overrideprotected void OnCreate (Bundle savedinstancestate {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} @SuppressWarnings ("deprecation") public void OnClick (View v) {showDialog (0);//When the button is clicked, the dialog box is displayed, and this method takes an integer parameter, Used to identify the specific dialog box to be displayed (only the dialog box labeled 0 is created). The @Override @deprecated//oncreatedialog () method is a callback method that is used to create a dialog box that is managed by the activity. This callback method is called when ShowDialog () is called. protected Dialog oncreatedialog (int id) {//TODO auto-generated method Stubswitch (ID) {Case 0://to create a dialog box, you need to use the Alertdialog class's builder constructor to set different properties, labels, headings, buttons, and check boxes. return new Alertdialog.builder (this)//Call the Alertdialog object's Builder constructor: SetIcon (R.drawAble.ic_launcher). Settitle ("This was a dialog with some simple text ..."). Setpositivebutton ("OK",//Set OK button new Dialoginterface.onclicklistener () {//Set click event @overridepublic void OnClick (Dialoginterface dialog,int which) {//TODO Auto-generated Method Stubtoast.maketext (Getbasecontext (), "OK clicked!", Toast.length_short). Show ();}}). Setnegativebutton ("Cancel",//Set Cancel button New Dialoginterface.onclicklistener () {//Set click event @overridepublic void OnClick (Dialoginterface dialog,int which) {//TODO auto-generated method Stubtoast.maketext (Getbasecontext (), "Cancel clicked!", Toast.length_short). Show ();}}). Setmultichoiceitems (items, itemschecked,//set check box new Dialoginterface.onmultichoiceclicklistener () {// Set Click event @overridepublic void OnClick (Dialoginterface dialog,int which, Boolean isChecked) {//TODO auto-generated Method St Ubtoast.maketext (Getbasecontext (), items[which]//selects the returned message by passing in the parameter ischecked to verify whether it is clicked. + (isChecked?) "checked!": "unchecked!"), Toast.length_short). Show ();}}). Create ();} return null;} @OverridepublIC boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is Present.getmenu Inflater (). Inflate (R.menu.main, menu); return true;}}

3, run a bit, display such as:


Click the button:


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.