AlertDialog.Builder 提供了如下方法:
setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener)
方法:来实现类似RadioButton的AlertDialog
第一个参数是要显示的数据的数组,
第二个参数是初始值(初始被选中的item),
第三个参数是点击某个item的触发事件
代码片:
+ private ArrayList<Integer> mTitleList = new ArrayList<Integer>();
+ private List mStoragePathList = new ArrayList();
+ public static final int INTERNAL_STORAGE_ITEM = 0;
+ public static final int EXTERNAL_STORAGE_ITEM = 1;
+ private int mCheckedItem = -1;
+ private String mStoragePath = "";
+ private void getCheckedStoragePath(int flag) {
+ if((flag & ActivityUtils.INTERNAL_STORAGE_AVAILABLE) !=0 ){
+ mStoragePathList.add(this.getResources().getString(R.string.title_section_internal)); // title_section_internal
+ }
+ if (UserHandle.myUserId() == UserHandle.USER_OWNER) {
+ if ((flag & ActivityUtils.EXTERNAL_STORAGE_AVAILABLE) != 0) {
+ mStoragePathList.add(this.getResources().getString(R.string.title_section_external)); // title_section_external
+ }
+ if ((flag & ActivityUtils.USB_STORAGE_AVAILABLE) != 0) {
+ mStoragePathList.add(this.getResources().getString(R.string.title_section_usbdisk)); // title_section_usbdisk
+ }
+ }
+ }
----------------------------------- 使用-----------------
// 初始化Dialog对话框单选内容 String list数据
+ getCheckedStoragePath(ActivityUtils.getAvailableStatus());
+ new AlertDialog.Builder(this)
+ .setTitle(R.string.select_destination)
+ .setSingleChoiceItems((String[]) (mStoragePathList.toArray(new String[mStoragePathList.size()])),mCheckedItem,
+ new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int which) {
// 某个项被点击后,保存位置信息
+ mCheckedItem = which;
+ }
+ })
+ .setPositiveButton("Ok",
+ new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int which) {
// ok确认按钮点击后,处理被选中的项
+ if(mCheckedItem!=-1) init(mCheckedItem);
+ }
+ })
+ .setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
dialog.dismiss();
finish();
}
+ }).show();