Lecture 44th: Android Dialog box (1)

Source: Internet
Author: User

Lecture 44th: Android Dialog box (1)

Lifelong learning, continuous reading, and books are the only choice that each of us will not let our lives wither too early. In order to make the tree of Our Lives evergreen and make our future life more colorful, friends, let's work together!


Description: Dialog box

1. Introduction to Dialog
Dialog is also a common user interface element in Android. It is not a subclass of View like Menu. Let's take a look at its inheritance relationship:



DatePickerDialog, ProgressDialog, and TimePickerDialog, which we have already discussed in the previous chapter.


Example 1: a prompt is displayed when you press the return button to ensure correct operations. A Common Dialog Box style is used.

The main interface file MainActivity. java is as follows:

Public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} protected void dialog () {// define the dialog box object AlertDialog. builder builder = new Builder (MainActivity. this); builder. setMessage ("are you sure you want to exit? "); Builder. setTitle ("prompt"); builder. setPositiveButton ("OK", new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {dialog. dismiss (); // close the MainActivity dialog box. this. finish (); // exit}); builder. setNegativeButton ("cancel", new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {dialog. dismiss () ;}}); builder. create (). show (); // display dialog box} // call this method in the onKeyDown (int keyCode, KeyEvent event) method @ Overridepublic boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK & event. getRepeatCount () = 0) {dialog () ;}return false ;}}

The running result is as follows:



Example 2: changed the chart in the dialog box and added three buttons.

The main interface file MainActivity. java is as follows:

Public class MainActivity extends Activity {private Button B; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); B = (Button) findViewById (R. id. button); B. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View arg0) {Dialog () ;}});} private void diid () {Dialog = new AlertDialog. builder (th Is). setIcon (android. R. drawable. btn_star). setTitle ("Preferences"). setMessage ("do you like her? "). SetPositiveButton ("very fond", new OnClickListener () {@ Overridepublic void onClick (DialogInterface arg0, int arg1) {Toast. makeText (MainActivity. this, "I like her very much. ", Toast. LENGTH_LONG ). show ();}}). setNegativeButton ("dislike", new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {Toast. makeText (MainActivity. this, "I don't like her. ", Toast. LENGTH_LONG ). show ();}}). setNeutralButton ("General", new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {Toast. makeText (MainActivity. this, "I do not like it or not. ", Toast. LENGTH_LONG). show () ;}}). create (); dialog. show ();}}

The running result is as follows:



Here is the lecture, Take your time and enjoy it

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.