DialogFragment的onClick监听器无法与ImageButton一起使用

3

我认为我已经尝试了这里提供的所有方法。我的活动中有一个ImageButton,但我无法将其设置为切换到另一个活动(Log.d显示它甚至没有被点击)。

这是我的对话框:

    public class StarsActivity extends DialogFragment implements OnClickListener {

    Dialog dialog;
    Activity mActivity;

    ImageButton nextQuestion;

    final String LOG_TAG = "myLogs";

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_stars, null);

        setCancelable(true);
        return view;
    }

    /** The system calls this only when creating the layout in a dialog. */
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // The only reason you might override this method when using onCreateView() is
        // to modify any dialog characteristics. For example, the dialog includes a
        // title by default, but your custom layout might not need it. So here you can
        // remove the dialog title, but you must call the superclass to get the Dialog.
        dialog = super.onCreateDialog(savedInstanceState);

        //Setting transparency for dialog background
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        //No title for dialog
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        dialog.setContentView(R.layout.activity_stars);
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);


        nextQuestion = (ImageButton)dialog.findViewById(R.id.nextQuestion);
        Log.d(LOG_TAG, nextQuestion.toString());
        nextQuestion.setOnClickListener(this);

        mActivity = getActivity();

        return dialog;
    }


    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.nextQuestion:
                Log.d(LOG_TAG, "Dialog 2: ");
                mActivity = getActivity();
                Intent i = new Intent(mActivity, FinalActivity.class);
                mActivity.startActivity(i);
                this.dismiss();
                break;
            default:
                break;
        }
    }
}

Here's my ImageButton xml:

<ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/nextQuestion"
            android:background="@null"
            android:src="@drawable/ic_content_nextbutton_02"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="20dp" />

我希望有一种方法可以解决这个问题。

我无法理解你的问题,请你能否澄清一下你的问题。 - Bhavdip Sagar
2个回答

4
我认为在已经继承自Dialog fragment的StarsActivity中无需创建一个Dialog实例。以下代码可以满足您的需求。
public class StarsActivity extends DialogFragment implements View.OnClickListener {
    Activity mActivity;
    ImageButton nextQuestion;

    final String LOG_TAG = "myLogs";

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_stars, null);
        nextQuestion = (ImageButton)view.findViewById(R.id.nextQuestion);
        Log.d(LOG_TAG, nextQuestion.toString());
        nextQuestion.setOnClickListener(this);
        setCancelable(true);
        return view;
    }


    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.nextQuestion:
                Log.d(LOG_TAG, "Dialog 2: ");
                mActivity = getActivity();
                Intent i = new Intent(mActivity, FinalActivity.class);
                mActivity.startActivity(i);
                this.dismiss();
                break;
            default:
                break;
        }
    }
}

请考虑将StarsActivity重命名为StarsDialog或其他名称。

1
谢谢,但问题是我需要我的对话框具有以下功能:没有标题,透明背景,具有父级宽度,而使用onCreateView无法使其正常工作。 - Nick Pakhomov
我设法让它工作,并保留了onCreateDialog()方法。谢谢! - Nick Pakhomov

0
在你的OnCreate方法中添加这行代码:
nextQuestion.setOnClickListener(this);

onCreate方法中,您无法引用DialogFragment的布局ID。该片段的视图仅在onCreateDialog方法中设置,然后才能引用这些ID。 - Ali Kazi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接