我遇到了一个问题,并且不知道在AlertDialog中使用一组drawable的最佳方法。当用户按下按钮并提示从可绘制列表中进行选择时,将显示AlertDialog。
目前它显示了在创建对话框时我在dialog_view布局中放置的按钮。理想情况下,我希望能够使用某种类型的列表视图,但如果不可能,我希望能够在AlertDialog内部处理项目的选择/按下。
如何处理AlertDialog中的任何操作?
DIALOGFRAGMENT
public class PicturePickerFragment extends DialogFragment {
ArrayList<Integer> imageList = new ArrayList<Integer>();
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// fill an array with selected images
String title = "Picture";
imageList.add(R.drawable.barbershop);
imageList.add(R.drawable.wedding);
imageList.add(R.drawable.meeting);
imageList.add(R.drawable.barbershop);
// return alertdialog
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_view, null))
.setTitle(R.string.event_type)
.setPositiveButton(R.string.select_picture,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// call the method on the parent activity when
// user click the positive button
}
});
return builder.create();
}
}
对话视图
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_baby"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/baby"
android:text="Baby Shower" />
<Button
android:id="@+id/btn_baking"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/baking"
android:text="Baking" />
<Button
android:id="@+id/btn_barber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/barbershop"
android:text="Barbershop" />
</LinearLayout>
答案 0 :(得分:2)
你可以使用一些ListAdapter,例如ArrayAdapter,你可以为列表中的每个项目提供一个drawable,然后在构建AlertDialog时使用适配器:
builder.setAdapter(myAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//which is the position of the item clicked
}
})
编辑:这是一个适用于imageList的示例:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setAdapter(new ArrayAdapter<Integer>(getActivity(), R.layout.dialog_image, imageList) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater inflater = getActivity().getLayoutInflater();
view = inflater.inflate(R.layout.dialog_image, parent, false);
} else {
view = convertView;
}
ImageView imageView = (ImageView) view.findViewById(R.id.image);
int resId = getItem(position);
imageView.setImageResource(resId);
return view;
}
}, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("Dialog", "selected position " + which);
}
});
return builder.create();
其中R.layout.dialog_image
包含一个标识为android:id="@+id/image"
的ImageView作为根元素。例如:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="center"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:minHeight="?attr/listPreferredItemHeight" >
</ImageView>