如何通过选择获得四个图像的警报对话框

时间:2017-03-31 12:32:12

标签: android

我想创建一个像附加图像的对话框按钮。 在此对话框中,将出现四个图像供选择。

enter image description here

String[] image = new String[]{R.mipmap.ic_launcher1, R.mipmap.ic_launcher2,R.mipmap.ic_launcher3, R.mipmap.ic_launcher4};
 AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Choose a container size");
        builder.setItems(image, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // the user clicked on colors[which]
            }
        });
        builder.show();

2 个答案:

答案 0 :(得分:2)

你可以在DialogBox里面使用Gridview来显示选择的图像,请试试这个

对话框:

  final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.layout_dialog);

    GridView gridView = (GridView)dialog.findViewById(R.id.dialog_gv);
    gridView.setAdapter(new DialogBaseAdapter());
    dialog.setTitle("Your title");
    dialog.setCancelable(true);
    dialog.show();

对话框视图xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<GridView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="2"
    android:stretchMode="columnWidth"
    android:columnWidth="72dp"
    android:id="@+id/dialog_gv"
    android:horizontalSpacing="2dp"
    android:verticalSpacing="2dp"></GridView>

</LinearLayout>

DialogBaseAdapter.class

class DialogBaseAdapter extends BaseAdapter{
    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = LayoutInflater.from(context).inflate(R.layout.dialog_grid_item, viewGroup,false);
        return view;
    }
}

Dialog Grid Adapter项目xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="96dp"
android:orientation="vertical">


<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/app_header_color"
    android:src="@drawable/arrow" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" />
</RelativeLayout>

答案 1 :(得分:1)

你必须使用android.App.Dialog

例如

  final Dialog dialog = new Dialog(this, R.style.cust_dialog);
    dialog.setContentView(R.layout.your_xml);
    dialog.setTitle("Your title");
    dialog.setCancelable(false);
    dialog.show();
   Button d_btn_ok = (Button) dialog.findViewById(R.id.btnOk);
   Button d_btn_cancel = (Button) dialog.findViewById(R.id.btnCancel);

   ImageView image1 = (ImageView) dialog.findViewById(R.id.image1);
   image1.setImageDrawable(getResources().getDrawable(R.drawable.address));

   ImageView image2 = (ImageView) dialog.findViewById(R.id.image2);

   ImageView image3 = (ImageView) dialog.findViewById(R.id.image3);

   ImageView image4 = (ImageView) dialog.findViewById(R.id.image4);

使xml具有ImageViews和你想要的东西

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:background="@color/lightest_grey"
android:theme="@style/AppTheme">

<TextView
    android:id="@+id/tvMsg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="18sp"
    android:gravity="center"
    android:text="Msg" />


<LinearLayout
    android:id="@+id/linearImage1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/tvMsg"
    android:weightSum="2">

    <ImageView
        android:id="@+id/image1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/image2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" />


</LinearLayout>

<LinearLayout
    android:id="@+id/linearImag2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/linearImage1"
    android:weightSum="2">

    <ImageView
        android:id="@+id/image3"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/image4"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" />


</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/linearImag2"
    android:weightSum="2">

    <Button
        android:id="@+id/btnOk"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/colorAccent"
        style="?android:attr/buttonBarButtonStyle"
        android:text="Yes"
        android:textColor="@color/white" />

    <Button
        android:id="@+id/btnCancel"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginLeft="2dp"
        android:layout_marginStart="2dp"
        style="?android:attr/buttonBarButtonStyle"
        android:background="@color/colorAccent"
        android:text="No"
        android:textColor="@color/white" />

</LinearLayout>