AlertDialog six creation methods and alertdialog six
AlertDialog six creation methods
To create AlertDialog, follow these steps:
1. Create an AlertDialog. Builder object
2. Call the setTitle method of the Builder object to set the title and setIcon method to set the icon.
3. Call Builder related methods such as setMessage, setItems, setSingleChoiceItems, setMultiChoiceItems, setAdapter, and setView to set different types of dialog box content.
4. Call setPositiveButton, setNegativeButton, and setNeutralButton to set multiple buttons.
5. Call the create () method of the Builder object to create the AlertDialog object
6. Call the show () method of the AlertDialog object to display the dialog box.
1. setMessage: Set the dialog box content to simple text content.
// Create the builder AlertDialog. builder builder = new AlertDialog. builder (this); // set the parameter builder. setTitle ("Please select "). setIcon (R. drawable. ic_launcher ). setMessage ("My beauty "). setPositiveButton ("", new OnClickListener () {// actively @ Override public void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stub Toast. makeText (MainActivity. this, "Congratulations!", 0 ). show ();}}). setNegativeButton ("not beautiful", new OnClickListener () {// negative @ Override public void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stub Toast. makeText (MainActivity. this, "not honest at all", 0 ). show ();}}). setNeutralButton ("unknown", new OnClickListener () {// intermediate @ Override public void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stub Toast. makeText (MainActivity. this, "Open your eyes quickly", 0 ). show () ;}}); builder. create (). show ();
Run:
Ii. setItem: Set the text box content as a simple list item
// Create data final String [] items = new String [] {"Beijing", "Shanghai", "Guangzhou", "Shenzhen"}; // create a dialog box builder AlertDialog. builder builder = new AlertDialog. builder (this); // set the parameter builder. setIcon (R. drawable. ic_launcher ). setTitle ("prompt "). setItems (items, new OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {Toast. makeText (MainActivity. this, items [which], Toast. LENGTH_SHORT ). show () ;}}); builder. create (). show ();
Run:
3. The content of the setSingleChoiceItems () Setting dialog box is a single-choice list item.
// Create data final String [] items = new String [] {"Beijing", "Shanghai", "Guangzhou", "Shenzhen"}; // create a dialog box builder AlertDialog. builder builder = new AlertDialog. builder (this); // set the parameter builder. setIcon (R. drawable. ic_launcher ). setTitle ("prompt "). setSingleChoiceItems (items, 0, new OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stub Toast. makeText (MainActivity. this, items [which], Toast. LENGTH_SHORT ). show () ;}}); builder. create (). show ();
Run:
IV. The content of the setMultiChoiceItems () Setting dialog box is a multi-Option List.
// Create data final String [] items = new String [] {"Beijing", "Shanghai", "Guangzhou", "Shenzhen"}; // create a dialog box builder AlertDialog. builder builder = new AlertDialog. builder (this); builder. setIcon (R. drawable. ic_launcher ). setTitle ("prompt "). setMultiChoiceItems (items, new boolean [] {true, true, false}, new OnMultiChoiceClickListener () {@ Override public void onClick (DialogInterface dialog, int which, boolean isChecked) {// TODO Auto-generated method stub if (isChecked) {Toast. makeText (MainActivity. this, items [which], 0 ). show () ;}}); builder. create (). show ();
Run:
5. Set the setAdapter () dialog box to a custom list item (a layout here)
// Create data final String [] items = new String [] {"Beijing", "Shanghai", "Guangzhou", "Shenzhen"}; // create a dialog box builder AlertDialog. builder builder = new AlertDialog. builder (this); builder. setTitle ("prompt "). setIcon (R. drawable. ic_launcher ). setAdapter (new ArrayAdapter <String> (MainActivity. this, R. layout. item, R. id. TV, items), new OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stub Toast. makeText (MainActivity. this, items [which], 0 ). show () ;}}); builder. create (). show ();
Run:
6. Set setView () to custom View
// Create a dialog box builder AlertDialog. builder builder = new AlertDialog. builder (this); // get the Layout View view2 = View. inflate (MainActivity. this, R. layout. login, null); // obtain the final EditText username = (EditText) view2.findViewById (R. id. username); final EditText password = (EditText) view2.findViewById (R. id. password); final Button button = (Button) view2.findViewById (R. id. btn_login); // sets the parameter builder. setTitle ("Login "). setIcon (R. drawable. ic_launcher ). setView (view2); // create a dialog box final AlertDialog alertDialog = builder. create (); button. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub String uname = username. getText (). toString (). trim (); String psd = password. getText (). toString (). trim (); if (uname. equals ("shenhua") & psd. equals ("123456") {Toast. makeText (MainActivity. this, "Logon successful", 0 ). show ();} Toast. makeText (MainActivity. this, "Logon Failed", 0 ). show (); alertDialog. dismiss (); // the dialog box disappears}); alertDialog. show ();
Run:
You blind