AlertDialog and androidalertdialog in android Development

Source: Internet
Author: User

AlertDialog and androidalertdialog in android Development

Several common methods:

1. setTitle ()

Set the title displayed in the dialog box.

2. setIcon ()

Set the icon in the dialog box. It is worth noting that if the setTitle () method is not used, setIcon () does not take effect.

3. setMessage ()

Set content in the dialog box.

4. setPositiveButton (), setNegativeButton (), setNeutralButton ()

Set the button in the dialog box.

5. setCancelable ()

In addition to the dialog box, the dialog box does not disappear, and the press return key dialog box does not disappear.


Prompt dialog box


Demo instance:

MainActivity. java

Public class MainActivity extends Activity {Button button; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void click (View v) {AlertDialog. builder builder = new AlertDialog. builder (MainActivity. this); builder. setMessage ("this is a prompt box"); builder. setIcon (R. drawable. icon); builder. setTitle ("prompt"); builder. setPositiveButton ("Positive", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface arg0, int arg1) {}}); builder. setNegativeButton ("Negative", new OnClickListener () {@ Overridepublic void onClick (DialogInterface arg0, int arg1) {// TODO Auto-generated method stub}); builder. setNeutralButton ("Neutral", new OnClickListener () {@ Overridepublic void onClick (DialogInterface arg0, int arg1) {// TODO Auto-generated method stub}); builder. show ();}}

Activity_main.xml

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = "$ {relativePackage }. $ {activityClass} "> <Button android: id =" @ + id/but "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "click" android: onClick = "click"/> </LinearLayout>

List dialog box


Demo instance:

MainActivity. java

Public class MainActivity extends Activity {Button button; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void click (View v) {AlertDialog. builder builder = new AlertDialog. builder (MainActivity. this); // builder. setMessage ("prompt dialog box"); after setMessage is set, setItems becomes invalid builder. setIcon (R. drawable. icon); builder. setTitle ("prompt"); builder. setItems (new String [] {"one", "two", "three"}, new OnClickListener () {@ Overridepublic void onClick (DialogInterface arg0, int index) {String str = null; switch (index) {case 0: str = "one"; break; case 1: str = "two"; break; case 2: str = "three"; break;} Toast. makeText (MainActivity. this, str, Toast. LENGTH_SHORT ). show () ;}}); builder. show ();}}

Activity_main.xml

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = "$ {relativePackage }. $ {activityClass} "> <Button android: id =" @ + id/but "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "click" android: onClick = "click"/> </LinearLayout>

Radio List dialog box


Demo instance:

MainActivity. java

Public class MainActivity extends Activity {Button button; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void click (View v) {AlertDialog. builder builder = new AlertDialog. builder (MainActivity. this); builder. setIcon (R. drawable. icon); builder. setTitle ("prompt"); builder. setSingleChoiceItems (new String [] {"one", "two", "three"}, 0, new OnClickListener () {@ Overridepublic void onClick (DialogInterface arg0, int index) {String str = null; switch (index) {case 0: str = "one"; break; case 1: str = "two"; break; case 2: str = "three"; break;} Toast. makeText (MainActivity. this, str, Toast. LENGTH_SHORT ). show () ;}}); builder. setPositiveButton ("OK", null); builder. show ();}}
Activity_main.xml

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = "$ {relativePackage }. $ {activityClass} "> <Button android: id =" @ + id/but "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "click" android: onClick = "click"/> </LinearLayout>

Check List dialog box


Demo instance:

MainActivity. java

Public class MainActivity extends Activity {Button button; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void click (View v) {AlertDialog. builder builder = new AlertDialog. builder (MainActivity. this); builder. setIcon (R. drawable. icon); builder. setTitle ("prompt"); final boolean [] items = new boolean [] {false, false, false}; builder. setMultiChoiceItems (new String [] {"one", "two", "three"}, items, new OnMultiChoiceClickListener () {@ Overridepublic void onClick (DialogInterface arg0, int arg1, boolean isCheaked) {}}); builder. setPositiveButton ("OK", null); builder. show ();}}
Activity_main.xml

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = "$ {relativePackage }. $ {activityClass} "> <Button android: id =" @ + id/but "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "click" android: onClick = "click"/> </LinearLayout>
 


Custom dialog box



MainActivity. java

Public class MainActivity extends Activity {Button button; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void click (View v) {AlertDialog. builder builder = new AlertDialog. builder (MainActivity. this); builder. setTitle ("prompt"); builder. setIcon (R. drawable. icon); ImageView imageView = new ImageView (MainActivity. this); ImageView. setImageResource (R. drawable. close); // obtain the layout File View myView = this. getLayoutInflater (). inflate (R. layout. form, null); builder. setView (myView); ListView listView = (ListView) myView. findViewById (R. id. lv); ArrayAdapter <String> adapter = new ArrayAdapter <String> (MainActivity. this, android. r. layout. simple_list_item_1, new String [] {"1", "2", "3"}); listView. setAdapter (adapter); listView. setOnItemClic KListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView <?> Arg0, View arg1, int positon, long arg3) {Toast. makeText (MainActivity. this, "" + positon, Toast. LENGTH_SHORT ). show () ;}}); builder. setPositiveButton ("OK", null); builder. show ();}}

Activity_main.xml

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = "$ {relativePackage }. $ {activityClass} "> <Button android: id =" @ + id/but "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "click" android: onClick = "click"/> </LinearLayout>
Form. xml (layout content displayed in the dialog box)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ListView         android:id="@+id/lv"        android:layout_width="fill_parent"        android:layout_height="fill_parent"></ListView>    </LinearLayout>


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.