如何用自定义对话框列表替换默认的android对话框列表

时间:2015-10-19 03:50:47

标签: java android android-layout dialog android-custom-view

我为layout dialog设计了一个自定义list,用户只能选择list中的一个选项,我想用它来代替默认dialog。我真的不知道该怎么做。 我在下面显示了fragment interface;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.widget.Toast;`


public class SingleChoiceClass extends DialogFragment {`

    final CharSequence[] items = {"b1", "b2", "b3", "b4"};
    String selection;
    @NonNull
    @Override


    public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());`


    builder.setTitle("Choose").setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface arg0, int arg1) {
        switch (arg1){
            case 0:
                selection = (String) items[arg1];
                break;
            case 1:
                selection = (String) items[arg1];
                break;
            case 2:
                selection = (String) items[arg1];
                break;
            case 3:
                selection = (String) items[arg1];
                break;

                }

        }
    }).setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {


            // Display toast with the user's selection
            Toast.makeText(getActivity(), "Your choice is : " + selection, Toast.LENGTH_SHORT).show();

            }
        });
        return builder.create();
     }
    }

我的自定义layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="350dp"
    android:layout_height="350dp"
    android:background="@color/button_material_light"
    android:layout_gravity="center">


    <TextView
        android:layout_width="350dp"
        android:layout_height="50dp"
        android:text="@string/textview"
        android:layout_gravity="top"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:layout_marginTop="20dp"
        android:textSize="30sp"
        android:textAlignment="center"
        android:textStyle="normal"
        android:id="@+id/textview" />


    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:text="@string/radio1"
            android:checked="false"
            android:id="@+id/radio1"
            android:textSize="25sp"
            android:background="#2bf308"
            android:clickable="true" />
        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:checked="false"
            android:text="@string/radio2"
            android:id="@+id/radio2"
            android:textSize="25sp"
            android:background="#f4fd02"
            android:clickable="true" />
        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:text="@string/radio3"
            android:checked="false"
            android:id="@+id/radio3"
            android:textSize="25sp"
            android:background="#fb8e35"
            android:clickable="true" />
        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:text="@string/radio4"
            android:checked="false"
            android:id="@+id/radio4"
            android:textSize="25sp"
            android:background="#fc3434"
            android:clickable="true" />
    </RadioGroup>

    <Button
        android:layout_width="match_parent"
        android:layout_height="65dp"
        android:text="@string/button"
        android:id="@+id/button2"
        android:layout_gravity="center_horizontal"
        android:textSize="25sp"
        android:clickable="true" />

</LinearLayout>

代码工作正常,但我需要使用自定义dialog而不是默认值。提前谢谢。

2 个答案:

答案 0 :(得分:1)

请试试这个

    //Please try this
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog dailog = new AlertDialog(getActivity());`

        LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.custom_layout, null);

        dialog.setTitle("Choose").setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                switch (arg1){
                case 0:
                    selection = (String) items[arg1];
                    break;
                case 1:
                    selection = (String) items[arg1];
                    break;
                case 2:
                    selection = (String) items[arg1];
                    break;
                case 3:
                    selection = (String) items[arg1];
                    break;

                }

            }
        }).setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {


                // Display toast with the user's selection
                Toast.makeText(getActivity(), "Your choice is : " + selection, Toast.LENGTH_SHORT).show();

            }
        });
        dialog.setView(view);
        return view;
    }

答案 1 :(得分:1)

我正在更新我的答案,因为您想要ListView作为自定义视图。首先在custom布局中添加如下所示的listView:

<ListView
   android:id="@+id/listView"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" >
</ListView> 

接下来,您需要创建一个adapter设置为创建ListView,您可以在onCreateDialog()方法中执行以下操作

public Dialog onCreateDialog(Bundle savedInstanceState) {

    ListView listView;

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.custom_layout, null);
    listView = (ListView)dialogView.findViewById(R.id.listView); // inflating from custom layout.


    builder.setView(dialogView);

}

此外,您需要在适配器的Adapter方法中为行创建inflategetView()布局,然后将该适配器设置为ListView

要处理listView中的点击事件,您可以这样做,

  listView.setOnItemClickListener(new OnItemClickListener() {

                  @Override
                  public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {


                  }
          });