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