Android Study Notes (7) -- display dialog box window

Source: Internet
Author: User

Show dialog box window


1. Create a Dialog1 project and add a Button in the activity_main.xml file:

     
  
 

2. Create a dialog box in the MainActivity. java file. 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", "Microsoft"}; boolean [] itemsChecked = new boolean [items. le Ngth]; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main) ;}@ SuppressWarnings ("deprecation") public void onClick (View v) {showDialog (0); // a dialog box is displayed when you click the button. This method accepts an integer parameter, used to identify the specific dialog box to be displayed (only a dialog box marked as 0 is created here ).} @ Override @ Deprecated // onCreateDialog () is a callback method used to create a dialog box 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: // create a Dialog box, you must use the Builder constructor of the AlertDialog class to set different attributes, such as labels, titles, buttons, and check boxes. Return new AlertDialog. Builder (this) // call the Builder constructor of the AlertDialog object .. SetIcon (R. drawable. ic_launcher ). setTitle ("This is a dialog with some simple text... "). setPositiveButton ("OK", // set OK button new DialogInterface. onClickListener () {// set the 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 the Cancel button to new DialogInterface. onClickListener () {// set the 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 the check box new DialogInterface. onMultiChoiceClickListener () {// set the Click Event @ Overridepublic void onClick (DialogInterface dialog, int which, boolean isChecked) {// TODO Auto-generated method stubToast. makeText (getBaseContext (), items [which] // use the input parameter isChecked to check whether the message is returned. + (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. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}

3. Run the command, as shown in:


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + teo797c0xawjuw.vcd4kpha + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140507/20140507091122215.jpg" alt = "">

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.