将AlertDialog正面按钮调用发送到自定义视图

时间:2017-07-12 23:37:55

标签: android alertdialog

我尝试将AlertDialog的{​​{1}}正面按钮事件发送到我的自定义视图DialogFragment,该视图在活动CustomViewGroup中嵌入了三次。< / p>

我按照https://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents的指南进行操作,该指南将正向按钮调用传递给活动中的重写方法Overview。在我的情况下,不需要在活动概述中处理此问题。

在AlertDialog中点击正向按钮后,如何调用自定义视图类的方法onDialogPositiveClick

以下结构应该给你一个概述(命名在这里概括):

活性

processInput

包含概览活动的布局:

public class Overview extends AppCompatActivity implements MyDialogFragment.DialogListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.overview);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final CustomViewGroup cvg1 = (CustomViewGroup) findViewById(R.id.first_view_group);
        // TODO Custom listener for onDialogPositiveClick needed?

        // TODO CustomViewGroup 2 and 3
    }

    @Override
    public void onDialogPositiveClick(DialogFragment dialog) {
        // Is called for each of my three CustomViewGroup instances 
        // after clicking positive button in AlertDialog.
    }

    @Override
    public void onDialogNegativeClick(DialogFragment dialog) {
        // Not needed
    }
}

CUSTOM VIEW GROUP

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

    <com.my.project.views.CustomViewGroup
        android:id="@+id/first_view_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.my.project.views.CustomViewGroup
        android:id="@+id/second_view_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.my.project.views.CustomViewGroup
        android:id="@+id/third_view_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

包含自定义视图组的布局:

public class CustomViewGroup extends LinearLayout {

        public CustomViewGroup(Context context, AttributeSet attrs) {
            super(context, attrs);

            // process some attributes, not relevant here, remove from layout too

            setOrientation(LinearLayout.HORIZONTAL);

            final AppCompatActivity activity = (AppCompatActivity) getContext();

            final LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflater.inflate(R.layout.custom_view_group, this, true);

            final Button mButton = (Button) getChildAt(1);
            mButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final DialogFragment dialog = CodeViewGroup.newInstance();
                    dialog.show(activity.getSupportFragmentManager(), "DialogFragment");
                }
            });
        }

        public static DialogFragment newInstance() {
            DialogFragment dialog = new MyDialogFragment();

            // pass arguments to dialog, not relevant here

            return dialog;
        }

        public void processInput(DialogFragment dialog) {
            // do some stuff with AlertDialog input
        }
    }

MYDIALOGFRAGMENT

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

            <TextView
                android:id="@+id/view_group_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/view_group_label_text" />

            <Button
                android:id="@+id/view_group_button"
                style="@style/Widget.AppCompat.Button.Colored"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/view_group_button_text" />

</merge>

with dialogfragment&#34; my_dialog_fragment&#34;:

public class MyDialogFragment extends DialogFragment {

    public interface DialogListener {
        void onDialogPositiveClick(DialogFragment dialog);

        void onDialogNegativeClick(DialogFragment dialog);
    }

    // Use this instance of the interface to deliver action events
    DialogListener mListener;    

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(getString(R.string.dialog_title))
                .setView(R.layout.my_dialog_fragment)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        mListener.onDialogPositiveClick(MyDialogFragment.this);
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        mListener.onDialogNegativeClick(MyDialogFragment.this);
                    }
                });
        // Create the AlertDialog object and return it
        return builder.create();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        Activity activity;

        // Verify that the host activity implements the callback interface
        try {
            if (context instanceof Activity) {
                activity = (Activity) context;
                // Instantiate the DialogListener so we can send events to the host
                mListener = (DialogListener) activity;
            } else {
                mListener = (DialogListener) context;
            }
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(context.toString()
                    + " must implement DialogListener");
        }
    }
}

希望重命名和合并后片段中没有拼写错误:)

1 个答案:

答案 0 :(得分:0)

为什么不使用AlertDialog显示带有自定义视图的对话框?

AlertDialog.Builder alert = new AlertDialog.Builder(this);

检查此链接以了解如何将AlertDialog与自定义视图一起使用。

这是一种非常简单有效的方法。

您可以在对话框中添加正按钮,负按钮,中性按钮。

http://android.pcsalt.com/create-alertdialog-with-custom-layout-using-xml-layout/