1.MainActivity.java
import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.content.res.Resources; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private Button mShowAlertDialog; private TextView mShowSelectedItemsOfAlertDialog; private String[] mItemsOfAlertDialog; private boolean[] mSelectedItems; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mItemsOfAlertDialog = getResources().getStringArray(R.array.dialog_items); mSelectedItems = new boolean[mItemsOfAlertDialog.length]; mShowAlertDialog = (Button) findViewById(R.id.show_alert_dialog); mShowSelectedItemsOfAlertDialog = (TextView) findViewById(R.id.show_selected_items); mShowAlertDialog.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { showAlertDialog(); } }); } private void showAlertDialog() { Resources resources = getResources(); AlertDialog.Builder ab = new AlertDialog.Builder(this); ab.setTitle(resources.getString(R.string.alert_dialog_name)); ab.setMultiChoiceItems(R.array.dialog_items, mSelectedItems, new MultiChoiceClickListener()); ab.setPositiveButton(resources.getString(R.string.ok), new ClickListenerForPositiveButton()); ab.setNegativeButton(resources.getString(R.string.exit), new ClickListenerForNegativeButton()); ab.show(); } private final class ClickListenerForNegativeButton implements OnClickListener { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } } private final class ClickListenerForPositiveButton implements OnClickListener { @Override public void onClick(DialogInterface dialog, int which) { String selectedItems = ""; for (int i = 0; i < mSelectedItems.length; ++i) { if (mSelectedItems[i]) { selectedItems = selectedItems + mItemsOfAlertDialog[i] + "\n"; } } mShowSelectedItemsOfAlertDialog.setText(getResources().getString( R.string.selected_items) + "\n" + selectedItems); dialog.dismiss(); } } private final class MultiChoiceClickListener implements OnMultiChoiceClickListener { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { mSelectedItems[which] = isChecked; } } }
Note:
showAlertDialog(), 其中setMultiChoiceItems()就是設定為Multi choice的重點, 第一個參數mItemsOfAlertDialog為soft coding帶入dialog item, dialog item定義在xml 中 如下
/res/values/arrays.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="dialog_items"> <item>item1</item> <item>item2</item> <item>item3</item> <item>item4</item> <item>item5</item> </string-array> </resources>
第2個參數 mSelectedItems 則是代表dialog item是否被選中
MultiChoiceClickListener類別則為監聽alertdialog中item被選中的事件,其實比較方便的寫法是直接建立匿名類別,但如此一來showAlertDialog()長度會被大幅增加, 可讀性降低
ClickListenerForNegativeButton類別則為監聽alertdialog中Negative Button被點擊的事件, 可以看到事件啟動之後僅把dialog dismiss,沒有作其他事情
ClickListenerForPositiveButton類別則為監聽alertdialog中Positive Button被點擊的事件, 事件啟動後檢查mSelectedItems的element,如果是true代表在dialog是被勾選的
2.main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/show_alert_dialog" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/show_alert_dialog" /> <TextView android:id="@+id/show_selected_items" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/selected_items" /> </LinearLayout>
匿名訪客
25 2 月, 2015 3:54 下午http://www.blogjava.net/Jack2007/archive/2008/05/10/199646.html
匿名訪客
25 2 月, 2015 3:59 下午http://download.csdn.net/detail/johnny901114/4905153