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 ;}}}}