自定义DialogFragment中的View.OnClickListener

时间:2015-01-04 21:00:50

标签: android android-view onclicklistener android-dialogfragment

我正在尝试按照this教程实施自定义DialogFragment。我的问题是我无法处理我的自定义视图的button.setOnClickListener事件。最奇怪的是我在设置按钮的.getText()时没有问题,我找不到处理点击事件的方法。贝娄是我的代码:

SettingsDialogFragment.java

 public class SettingsDialogFragment extends DialogFragment
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();
    final View view = inflater.inflate(R.layout.dialog_settings, null);

    final Button colorButton =(Button) view.findViewById(R.id.colorButton_dialogSettings);
    String s = colorButton.getText().toString();
    System.out.println("its working "+s);

    //NOT working
    colorButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            System.out.println("OnClick");
        }
    });

    // 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_settings, null))
            // Add action buttons
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id)
                {

                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    SettingsDialogFragment.this.getDialog().cancel();
                }
            });
    return builder.create();
}

` 我的自定义视图代码(dialog_settings.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="200dp"
          android:layout_height="wrap_content">
<EditText
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center"
    android:scaleType="center"
    android:background="#00CCCC"
    android:contentDescription="@string/app_name"
    android:text="@string/dialog_settings_title"
    android:id="@+id/editText"/>
<Button
    android:id="@+id/colorButton_dialogSettings"
    android:inputType="textEmailAddress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/color_picker_title"
    android:layout_below="@+id/editText"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    />

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Stroke"
    android:layout_marginLeft="55dp"
    android:id="@+id/radioButtonStroke"
    android:checked="false"
    android:layout_below="@+id/colorButton_dialogSettings"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"/>

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Fill"
    android:id="@+id/radioButton_fill"
    android:checked="false"
    android:layout_below="@+id/colorButton_dialogSettings"
    android:layout_toRightOf="@+id/radioButtonStroke"
    android:layout_toEndOf="@+id/radioButtonStroke"
    android:layout_marginLeft="10dp"
    />

2 个答案:

答案 0 :(得分:6)

我只是向您展示了重要的部分..我希望您在代码中找到各自的行

final View view = inflater.inflate(R.layout.dialog_settings, null);
// inflating your view..for drawback, this line is [A]

您的colorButton引用view.findViewById(R.id.colorButton_dialogSettings)来自视图组view ..您引用了onclick侦听器...

builder.setView(inflater.inflate(R.layout.dialog_settings, null))

此处的代码设置对话框的内容视图。它会扩展布局并使其正常工作..所以最后您的builder没有将内容视图引用到view但是新的R.layout.dialog_settings布局......

所以要解决它就这样做

builder.setView(view) // hope you know the view parameter

view是您在第[A]行实例化的内容。

希望我有道理,并且足够清楚。如果有帮助我就知道了。

答案 1 :(得分:0)

新答案

将您的onCreateDialog更改为:

import android.view.View.OnClickListener; 

public class SettingsDialogFragment extends DialogFragment implements onClickListener
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();
    final View view = inflater.inflate(R.layout.dialog_settings, null);

    final Button colorButton =(Button) view.findViewById(R.id.colorButton_dialogSettings);
    String s = colorButton.getText().toString();
    System.out.println("its working "+s);

    colorButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            switch (v.getId()) {
                case R.id.colorButton_dialogSettings
                    System.out.println("OnClick");
                    break;
                default:
                    break;
        }
    });

    // 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_settings, null))
            // Add action buttons
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id)
                {

                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    SettingsDialogFragment.this.getDialog().cancel();
                }
            });
    return builder.create();
}

在您的活动中:

private Button colorButton = (Button) findViewById(R.id.colorButton_dialogSettings);

**旧答案要求您自己编写showDialog method* *

尝试删除onCreateDialog中的按钮代码并添加:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_dialog, container, false);
        Button colorButton =(Button) v.findViewById(R.id.colorButton_dialogSettings);
            public void onClick(View v) {
            // When button is clicked, call up to owning activity.
                ((FragmentDialog)getActivity()).showDialog();
                System.out.println("OnClick");
            }
        });

        return v;