(Android review) Use of dialog, androiddialog
I. Basic knowledge points
Common dialog
Basic code:
AlertDialog. Builder builder = new AlertDialog. Builder (this );
AlertDialog dialog = builder. create ();
Dialog. show ();
1) Common Dialog Box
Builder. setMessage ("browse the website of Chuanzhi podcast ");
Builder. setPositiveButton
2) Select dialog box
Builder. setItems (items, new DialogInterface. OnClickListener ()
Items is an array of options.
3) single choice dialog box
Builder. setSingleChoiceItems (items, 0, new DialogInterface. OnClickListener ()
Second parameter: The selected button by default.
4) Multi-choice dialog box
Builder. setMultiChoiceItems (items, checkedItems, new DialogInterface. OnMultiChoiceClickListener ()
5) custom dialog box
LayoutInflater mInflater = LayoutInflater. from (this );
View view = mInflater. inflate (R. layout. custom_diater, null );
Builder. setView (view );
Ii. Sample Code
1. MainActivity
Package com. example. dialogtest; import android. OS. bundle; import android. app. activity; import android. app. alertDialog; import android. content. dialogInterface; import android. content. dialogInterface. onClickListener; import android. content. dialogInterface. onMultiChoiceClickListener; import android. view. layoutInflater; import android. view. menu; import android. view. view; import android. widget. button; import andro Id. widget. toast; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main);} public void commondialog (View view) {AlertDialog. builder builder = new AlertDialog. builder (this); builder. setCancelable (false); builder. setTitle ("goddess"); builder. setMessage ("is Zhang Zetian your goddess? "); Builder. setPositiveButton ("OK", new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {}}); builder. setNeutralButton ("hide", new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stub}); builder. setNegativeButton ("cancel", new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {}}); AlertDialog dialog = builder. create (); dialog. show ();} public void selectdialog (View view) {AlertDialog. builder builder = new AlertDialog. builder (this); final String items [] = new String [] {"Zhang Zetian", "Zhang Zetian 1", "Zhang Zetian 2", "Zhang Zetian 3"}; builder. setTitle ("select goddess"); builder. setItems (items, new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {Toast. makeText (getApplicationContext (), items [which], 0 ). show () ;}}); AlertDialog dialog = builder. create (); dialog. show ();} public void singleselectdialog (View view) {AlertDialog. builder builder = new AlertDialog. builder (this); builder. setTitle ("select goddess"); final String items [] = new String [] {"Zhang Zetian", "Zhang Zetian 1", "Zhang Zetian 2", "Zhang Zetian 3 "}; builder. setSingleChoiceItems (items, 0, new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {Toast. makeText (getApplicationContext (), items [which], 0 ). show () ;}}); builder. setPositiveButton ("OK", new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {}}); AlertDialog dialog = builder. create (); dialog. show ();} public void multiselectdialog (View view) {AlertDialog. builder builder = new AlertDialog. builder (this); final String items [] = new String [] {"Zhang Zetian", "Zhang Zetian 1", "Zhang Zetian 2", "Zhang Zetian 3 ",}; final boolean checkedItems [] = new boolean [] {true, false}; builder. setTitle ("select goddess"); builder. setMultiChoiceItems (items, checkedItems, new OnMultiChoiceClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which, boolean isChecked) {Toast. makeText (getApplicationContext (), items [which], 0 ). show () ;}}); builder. setPositiveButton ("OK", new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {}}); AlertDialog dialog = builder. create (); dialog. show ();} public void customdialog (View view) {LayoutInflater inflater = LayoutInflater. from (this); View mView = inflater. inflate (R. layout. custom_dialog, null); AlertDialog. builder builder = new AlertDialog. builder (this); builder. setTitle ("enter user information"); builder. setView (mView); final AlertDialog dialog = builder. create (); dialog. show (); Button okBtn = (Button) mView. findViewById (R. id. bt_ OK); okBtn. setOnClickListener (new View. onClickListener () {// note that this demo uses two OnClickListener. therefore, you need to add View @ Overridepublic void onClick (View v) {dialog. dismiss () ;}}) ;}@ 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. Download source code
Http://download.csdn.net/detail/caihongshijie6/7799073
IV,
How can android custom dialog style be applied to its own dialog, key code is required
1. Create a CustomTheme. xml file under res/value.
Redefines a CustomDialogTheme Style, inherits the Theme. Dialog of the system, and then overwrites the "android: windowBackground" and "buttonStyle" attributes.
Define a CustomTheme Style, inherit the Theme topic of the system, and re-define the dialogTheme item in CustomTheme to specify it as the newly created CustomDialogTheme.
2. Under the application tag in Manifest, specify the "android: theme" attribute as the newly defined customeme
Bytes ----------------------------------------------------------------------------------------------
For more questions and answers, please go to @ Android Sina Weibo
How to Implement the mode DIalog in android
Two methods
1 service sends a broadcast intent message, and other activities register the receiver er. Toast
2 service sends an intent to an activity, which starts and displays a prompt.
This activity should not be full screen and should not be included in history records. Only serves as a prompt.