Android: Common AlertDialog dialog box and custom dialog box

Source: Internet
Author: User

Android: Common AlertDialog dialog box and custom dialog box

The Common Dialog box includes a confirmation Dialog box, a single choice button Dialog box, a multiple choice button Dialog box, and a custom Dialog box.

Common AlertDialog Methods

SetTitle: Set the title for the dialog box

SetMessage: Set content for the dialog box

SetIcon: Set the icon for the dialog box

List to be displayed in the setItems Settings dialog box

SetMultiChoiceItems: generally used to display check boxes

SetSingleChoiceItem:, set the radio button

SetNeutralButton: normal button

SetPositiveButton: Add OK button

SetNegativeButton: Add cancel button

SetView: Set custom styles

Here is an example to learn about these methods.

Here is the running result:

MainActivity. class

Package com. example. alertdialog; import android. r. bool; import android. OS. bundle; import android. app. activity; import android. app. alertDialog; import android. app. alertDialog. builder; import android. app. dialog; import android. content. dialogInterface; import android. content. pm. labeledIntent; import android. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. wid Get. button; import android. widget. toast; public class MainActivity extends Activity implements OnClickListener {private Button button1; private Button button2; private Button button3; private Button button4; private Button button5; private int mark = 0; private String item [] = {"student", "worker", "instructor", "farmer"}; private String multChoice [] = {"Travel", "movie ", "Sports", "reading"}; @ Overrideprotected void onCreate (Bundle savedInstanceSt Ate) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button1 = (Button) findViewById (R. id. btn_button1); button2 = (Button) findViewById (R. id. btn_button2); button3 = (Button) findViewById (R. id. btn_button3); button4 = (Button) findViewById (R. id. btn_button4); button5 = (Button) findViewById (R. id. btn_button5); button1.setOnClickListener (this); button2.setOnClickListener (this); butt On3.setOnClickListener (this); button4.setOnClickListener (this); button5.setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubswitch (v. getId () {// confirmation dialog box case R. id. btn_button1: {AlertDialog. builder builder = new Builder (this); builder. setIcon (R. drawable. ic_launcher); builder. setTitle ("confirmation dialog box"); builder. setMessage ("are you sure you want to exit? "); Builder. setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stubToast. makeText (MainActivity. this, "you clicked OK", Toast. LENGTH_SHORT ). show () ;}}); builder. setNegativeButton ("cancel", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-gener Ated method stubToast. makeText (MainActivity. this, "you have clicked the cancel button", Toast. LENGTH_SHORT ). show () ;}}); builder. create (); builder. show (); break;} // List dialog box case R. id. btn_button2: {AlertDialog. builder builder = new Builder (this); builder. setIcon (R. drawable. ic_launcher); builder. setTitle ("Occupation"); builder. setItems (item, new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which ){ // TODO Auto-generated method stubToast. makeText (MainActivity. this, "your career is" + item [which], Toast. LENGTH_SHORT ). show () ;}}); builder. create (); builder. show (); break;} // multiple choice dialog box case R. id. btn_button3: {final boolean choose [] = new boolean [multChoice. length]; AlertDialog. builder builder = new Builder (this); builder. setIcon (R. drawable. ic_launcher); builder. setTitle ("hobbies"); builder. setMultiChoiceItems (multChoice, null, New DialogInterface. onMultiChoiceClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which, boolean isChecked) {// TODO Auto-generated method stubchoose [which] = isChecked;}); builder. setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stubString result = ""; for (int I = 0; I <m Ultchoice. length; I ++) {if (choose [I]) {= "" result + = "multChoice [I] +"; = ""} = "" toast. maketext (mainactivity. this, = "Your hobbies [" + result + "]", = "" toast. length_short ). show (); = ""}); = "" builder. create (); = "" builder. show (); = "" break; = "" Single-choice dialog box = "" case = "" r. id. btn_button4: = "" {= "" mark = "0;" alertdialog. builder = "" builder = "new" builder (this); = "" builder. seticon (r. drawable. ic_launcher); = "" builder. settitle ("Occupation "); = "" Builder. setsinglechoiceitems (item, = "" 0, = "" new = "" dialoginterface. onclicklistener () = "" @ override = "" public = "" void = "" onclick (dialoginterface = "" dialog, = "" int = "" which) = "" todo = "" auto-generated = "" method = "" stub = "" builder. setpositivebutton ("OK", = "Your Career:" + item [mark], = "" Custom dialog box = "" r. id. btn_button5: = "" layoutinflater = "" inflater = "LayoutInflater. from (this); "view =" "null); =" "builder. settit Le ("Custom dialog box"); = "" builder. setview (view); = "" builder. setneutralbutton ("normal button", = "" toast. maketext (mainactivity. this, "I am a custom dialog box", toast. length_short ). show (); = "" layout file <= "" pre = ""> <p> </p> <p> activity_main.xml </p> <pre class = "brush: java; "> <relativelayout 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: paddingbottom = "@ dimen/activity_vertical_margin" android: paddingleft = "@ dimen/plugin" android: paddingright = "@ dimen/plugin" android: paddingtop = "@ dimen/activity_vertical_margin" tools: context = ". mainActivity "> <linearlayout android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: orientation =" vertical "> <Button android: id = "@ + id/btn_button1" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "confirmation dialog box"> </button> <button android: id = "@ + id/btn_button2" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "List dialog box"> </button> <button android: id = "@ + id/btn_button3" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: te Xt = "Multi-choice dialog box"> </button> <button android: id = "@ + id/btn_button4" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "single choice dialog box"> </button> <button android: id = "@ + id/btn_button5" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "Custom dialog box"> </button> </linearlayout> </relativelayout> </pre> Custom dialog box layout file <p> </p> <p> item. xml </p> <pre class = "brush: jav A; "> <! -- {Cke_protected} {C} % 3C! % 2D % 2D % 3 Fxml % 20 version % 3D % 221.0% 20 encoding % 3D % 22utf-8% 22% 3F % 2D % 2D % 3E --> <linearlayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <imageview android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: scaletype = "fitCenter" android: src = "@ drawable/icon"> <textview android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "I Am a custom dialog box"> </textview> </imageview> </linearlayout> </pre> I hope to help you with slag learning, and <p> </multchoice. length; I ++) {>

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.