Android single-choice dialog box and multiple-choice dialog box

Source: Internet
Author: User

Main. xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:gravity="center_horizontal"    android:orientation="vertical"    >    <Button        android:id="@+id/singleChoiceButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/singleChoice"         android:layout_marginTop="100dip"        android:textSize="20sp"     />        <Button        android:id="@+id/multiChoiceButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/multiChoice"         android:layout_marginTop="100dip"        android:textSize="20sp"     /></LinearLayout>


 

MainActivity

Import android. OS. bundle; import android. app. activity; import android. app. dialog; import android. app. alertDialog. builder; import android. content. dialogInterface; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; public class MainActivity extends Activity {private Button mSingleChoiceButton; private Button mMultiChoiceButton; private Dialog mSingleDialog; private Dialog mMultiDialog; boolean singleFlags [] = null; boolean multiFlags [] = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); init ();} private void init () {mSingleChoiceButton = (Button) findViewById (R. id. singleChoiceButton); mSingleChoiceButton. setOnClickListener (new ButtonOnClickListenerImpl (); mMultiChoiceButton = (Button) findViewById (R. id. multiChoiceButton); mMultiChoiceButton. setOnClickListener (new ButtonOnClickListenerImpl ();} private class ButtonOnClickListenerImpl implements OnClickListener {@ Overridepublic void onClick (View view) {switch (View. getId () {case R. id. singleChoiceButton: singleFlags = new boolean [] {true, false, false}; Builder singleBuilder = new Builder (MainActivity. this); singleBuilder. setIcon (R. drawable. ic_launcher); singleBuilder. setTitle (getResources (). getString (R. string. dialog_title); singleBuilder. setSingleChoiceItems (R. array. singleChoiceArray, 0, new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {for (int I = 0; I <singleFlags. length; I ++) {if (I = which) {singleFlags [I] = true;} else {singleFlags [I] = false ;}}}); singleBuilder. setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {String result = ""; for (int I = 0; I <singleFlags. length; I ++) {if (singleFlags [I] = true) {result = getResources (). getStringArray (R. array. singleChoiceArray) [I] ;}} Toast. makeText (MainActivity. this, result, Toast. LENGTH_SHORT ). show () ;}}); mSingleDialog = singleBuilder. create (); mSingleDialog. show (); break; case R. id. multiChoiceButton: multiFlags = new boolean [] {true, false, false}; Builder multiBuilder = new Builder (MainActivity. this); multiBuilder. setIcon (R. drawable. ic_launcher); multiBuilder. setTitle (getResources (). getString (R. string. dialog_title); multiBuilder. setMultiChoiceItems (R. array. multiChoiceArray, multiFlags, new DialogInterface. onMultiChoiceClickListener () {@ Overridepublic void onClick (DialogInterface arg0, int which, boolean isChecked) {multiFlags [which] = isChecked ;}}); multiBuilder. setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int arg1) {String result = ""; for (int I = 0; I <multiFlags. length; I ++) {if (multiFlags [I]) {result = result + getResources (). getStringArray (R. array. multiChoiceArray) [I] + "" ;}} Toast. makeText (MainActivity. this, result, Toast. LENGTH_SHORT ). show () ;}}); mMultiDialog = multiBuilder. create (); mMultiDialog. show (); break; default: 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.