Android learning notes-Dialog explanation, android-dialog

Source: Internet
Author: User

Android learning notes-Dialog explanation, android-dialog

1. Use of the dialog box

1.1AlertDialog display

Simple dialog box and listener settings: focus on three buttons (that is, three words ):

PositiveButton (confirmation button); NeutralButton (ignore button)

AlertDialog. Builder bud1 = new Builder (mContext );

Bud1.setTitle ("prompt message ");

Bud1.setMessage ("your information has been submitted! ");

Bud1.setPositiveButton ("OK", new DialogInterface. OnClickListener (){

@ Override

Public void onClick (DialogInterface dialog, int which ){

Dialog. dismiss (); // the dialog box disappears.

}

});

Bud1.setPositiveButton ("cancel", new DialogInterface. OnClickListener (){

@ Override

Public void onClick (DialogInterface dialog, int which ){

Dialog. cancel (); // cancel the dialog box Toast. makeText (mContext, "you have canceled the session! ", Toast. LENGTH_LONG). show ();

}

});

Bud1.setNeutralButton ("Ignore", new DialogInterface. OnClickListener (){

@ Override

Public void onClick (DialogInterface dialog, int which ){

Toast. makeText (Code06_03.this, "you have ignored... ", Toast. LENGTH_LONG). show ();

}

});

// Select one of the radio buttons

// 1. Set the data source

Final String [] items = {"android", "ipone", "Symbian "};

// 2. Data adaptation

Bud1.setItems (items, new DialogInterface. OnClickListener (){

Public void onClick (DialogInterface dialog, int which ){

Toast. makeText (mContext, items [which], Toast. LENGTH_SHORT). show ();

}

});

 

 

// Custom dialog box

ImageView img = new ImageView (this );

Img. setImageResource (R. drawable. icon );

Bud1

. SetView (img)

 

Bud1.create (). show (); // displayed dialog box

 

 

 

1.2.ProgressDialog display

The key reason is that no error is reported when calling the UI operation in the Child thread. After analysis,

ProgressDialg has implemented Hanlder internally, so no error is reported. RunOnUiThread is also redundant.

 

Protected void dialog9 (){

New Thread (new Runnable (){

@ Override

Public void run (){

Try {

Thread. sleep (5*1000 );

// ProgressDialog. dismiss ();

RunOnUiThread (finishDialog );

} Catch (InterruptedException e ){

}

}

}). Start ();

ProgressDialog = ProgressDialog. show (Code06_07.this, "Please wait ",

"Data loading...", true );

}

 

Private Runnable finishDialog = new Runnable (){

@ Override

Public void run (){

ProgressDialog. dismiss ();

}

};

 

 

 

M_pDialog = new ProgressDialog (Code06_09.this );

// Set the progress bar style in a long shape

M_pDialog.setProgressStyle (ProgressDialog. STYLE_HORIZONTAL );

// Set the ProgressDialog title

M_pDialog.setTitle ("prompt ");

// Set ProgressDialog prompt information

M_pDialog.setMessage ("this is a long dialog progress bar ");

// Set the ProgressDialog title icon

M_pDialog.setIcon (R. drawable. icon );

 

// Set the progress bar of ProgressDialog

M_pDialog.setProgress (100 );

 

// Set whether the progress bar of ProgressDialog is clear

M_pDialog.setIndeterminate (false );

 

// Sets whether ProgressDialog can be canceled by pressing the return button.

M_pDialog.setCancelable (true );

 

// Display ProgressDialog

M_pDialog.show ();

 

New Thread (){

Public void run (){

Try {

While (m_count <= 100 ){

// The progress is controlled by the thread.

M_pDialog.setProgress (m_count ++ );

Thread. sleep (1000 );

}

M_pDialog.cancel ();

} Catch (InterruptedException e ){

M_pDialog.cancel ();

}

}

}. Start ();

 

}

 

When I was testing the code, I encountered an outdated method and had a new API for use.

 

 

/***
* The first parameter is used to determine which button is bound to the listener.
* @ Param whichButton Which button to set the listener on, can be one
* You can select the following constants:
* BUTTON_POSITIVE
* {@ Link DialogInterface # BUTTON_POSITIVE}
* {@ Link DialogInterface # BUTTON_NEGATIVE}
* {@ Link DialogInterface # BUTTON_NEUTRAL
**/
Progress. setButton (AlertDialog. BUTTON_POSITIVE, "OK", new DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog, int I ){
// Click "OK" to cancel the dialog box.
Dialog. cancel ();
}
});

After the above analysis and code writing, we can sum up the latency of ProgressDialog. The Handler mechanism can be used to safely delay in the Thread. The operations in the Thread include setProgress, dismiss.

1.3 custom dialog box-

LayoutInflater inflater = mContext. getLayoutInflater ();

MView = inflater. inflate (R. layout. diater, // custom dialog box View

Null );

 

AlertDialog. Builder (mContext). setView (mView );

 

The point that Fei Ge talked about today is about the use of two classes. The ProgressDialog and AlertDialog classes mainly explain how to use them and how to use ProgressDialog in the Thread.

 

1.4AlertDialog Parsing

AlertDialog extens Dialog implents DialogInterface

Three subclasses: DataPicker, ProgressDialog, and TimePickerDialog

Next, let's take a look at what surprises APIGuida has?

AlertDialog

A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

Through this sentence, I know that Feige is not easy and I have used all AlertDialog functions. the, I Knowed it, and did it

DatePickerDialog or TimePickerDialog

A dialog with a pre-defined UI that allows the user to select a date or time.

A pre-defined dialog box allows users to select a time or date

 

You shoshould use a DialogFragment as a container for your dialog. The DialogFragment class provides all the controls you need to create your dialog and manage its appearance, instead of calling methods on the Dialog object

I don't know how you think about this sentence. I feel like the code above is useless. Big Boss is DialogFragment! Don't worry, please refer to later.

To put it simply: 1. Correct Processing of the Dialog Lifecycle

2. simple use of the dialog box, like Fragment, as a component

3. Support for a wide range of Android and above at the API level. Of course, you need to support libraries.

Here, Google provides an example to encapsulate AlertDialog for reuse.

Public class FireMissilesDialogFragment extends DialogFragment {
@ Override
Public Dialog onCreateDialog (Bundle savedInstanceState ){
// Use the Builder class for convenient dialog construction
AlertDialog. Builder builder = new AlertDialog. Builder (getActivity ());
Builder. setMessage (R. string. dialog_fire_missiles)
. SetPositiveButton (R. string. fire, new DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog, int id ){
// Fire ze missiles!
}
})
. SetNegativeButton (R. string. cancel, new DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog, int id ){
// User canceled the dialog
}
});
// Create the AlertDialog object and return it
Return builder. create ();
}
}

After you get the instance, you can show

 

Refer:

FragmentDialog: Details: Portal

Andorid SDK Dialog

Additional reading:

Full Screen DialogFragment (over ActionBar) in Android: Portal

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.