Prompt & amp; dialog box in android ---- AlertDialog, android prompt

Source: Internet
Author: User

Prompt & dialog box in android ---- AlertDialog, android prompt

AlertDialog (dialog box)

I. Basic Procedure of using the dialog box

Step 1: Create AlertDialog. Buider;

Step 2: Call setIcon () to set the icon, setTitle () or setCustomerTitle to set the title

Step 3: Set the content of the dialog box, setMessage (), and other methods;

Step 4: setPosition/Negative/NaturalButton settings: OK, cancel, and neutral

Step 5: Call the create () method to create this object. After the show () method is called, the dialog box is displayed.

2. Examples of commonly used dialogs that have custom views

Package com. example. test3; import android. app. activity; import android. app. alertDialog; import android. content. dialogInterface; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. toast; public class MainActivity extends Activity implements View. onClickListener {private Button btn1; private Button btn2; private Button btn3; private Button btn4; private Bu Tton btn5; private AlertDialog. builder builder; private AlertDialog alertDialog; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btn1 = (Button) findViewById (R. id. btn1); btn2 = (Button) findViewById (R. id. btn2); btn3 = (Button) findViewById (R. id. btn3); btn4 = (Button) findViewById (R. id. btn4); btn5 = (Button) f IndViewById (R. id. btn5); listener (this); btn2.setOnClickListener (this); btn3.setOnClickListener (this); listener (this); btn5.setOnClickListener (this);} @ Override public void onClick (View view) {switch (view. getId () {case R. id. btn1: {builder = new AlertDialog. builder (MainActivity. this); alertDialog = builder. setIcon (R. mipmap. ic_launcher ). setTitle ("system prompt :"). setMessage ("This is the most common AlertDialog, \ n with three buttons, respectively, cancel, OK, neutral "). setNegativeButton ("cancel", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialogInterface, int I ){}}). setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialogInterface, int I ){}}). setNeutralButton ("neutral", new DialogInterface. onClickListener () {@ Overrid E public void onClick (DialogInterface dialogInterface, int I ){}}). create (); alertDialog. show (); break;} case R. id. btn2: {final String [] lesson = new String [] {"", "Mathematics", "English", "chemistry", "biology", "physics ", "Sports"}; builder = new AlertDialog. builder (MainActivity. this); alertDialog = builder. setIcon (R. mipmap. ic_launcher ). setTitle ("select your favorite course "). setItems (lesson, new DialogInterface. onClickListener () {@ Override Public void onClick (DialogInterface dialog, int which) {Toast. makeText (getApplicationContext (), "you have selected" + lesson [which], Toast. LENGTH_SHORT ). show ();}}). create (); alertDialog. show (); break;} case R. id. btn3: {final String [] fruits = new String [] {"apple", "Sydney", "banana", "grape", "watermelon"}; builder = new AlertDialog. builder (MainActivity. this); alertDialog = builder. setIcon (R. mipmap. ic_launcher ). setTitle ("select your favorite You can only select one fruit ~ "). SetSingleChoiceItems (fruits, 0, new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {Toast. makeText (getApplicationContext (), "you have selected" + fruits [which], Toast. LENGTH_SHORT ). show ();}}). create (); alertDialog. show (); break;} case R. id. btn4: {final String [] menu = new String [] {"boiled bean curd", "", "Soy Sauce Chicken", "Pepper pig chicken "}; // define a boolean array final boolean [] checkItems = new boolean [] {false, false, false}; builder = new AlertDialog. builder (MainActivity. this); alertDialog = builder. setIcon (R. mipmap. ic_launcher ). setMultiChoiceItems (menu, checkItems, new DialogInterface. onMultiChoiceClickListener () {@ Override public void onClick (DialogInterface dialog, int which, boolean isChecked) {checkItems [which] = isChecked ;}}). setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {String result = ""; for (int I = 0; I <checkItems. length; I ++) {if (checkItems [I]) result + = menu [I] + "";} Toast. makeText (getApplicationContext (), "you clicked" + result, Toast. LENGTH_SHORT ). show ();}}). create (); alertDialog. show (); break;} case R. id. btn5: {builder = new AlertDialog. builder (MainActivity. this); View view1 = getLayoutInflater (). inflate (R. layout. view_dialog, null); builder. setView (view1); alertDialog = builder. create (); alertDialog. show (); Button btn1 = (Button) view1.findViewById (R. id. btn_cancle); Button btn2 = (Button) view1.findViewById (R. id. btn_blog); Button btn3 = (Button) view1.findViewById (R. id. btn_close); btn1.setOnClickListener (new View. onClickListener () {@ Override public void onClick (View view) {alertDialog. dismiss () ;}}); btn2.setOnClickListener (new View. onClickListener () {@ Override public void onClick (View view) {alertDialog. dismiss () ;}}); btn3.setOnClickListener (new View. onClickListener () {@ Override public void onClick (View view) {alertDialog. dismiss () ;}}); break ;}}}}

 

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.