android - DialogFragment onClick 监听器不适用于 ImageButton

标签 android dialog onclick onclicklistener dialogfragment

我想我已经尝试了此处提供的所有内容。我的 Activity 中有 ImageButton,我无法将其设置为切换到另一个 Activity (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;
        }
    }
}

这是我的 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" />

希望有办法解决。

最佳答案

我认为您不需要在已经从 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 或其他名称。

关于android - DialogFragment onClick 监听器不适用于 ImageButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29715449/

相关文章:

android - 如何使用 RelativeLayout 以编程方式设置 textview 的重力?

android - 向图层列表添加渐变

android - 从另一个 Activity 返回后 DialogFragment 消失了

javascript - 在 jquery 对话框中插入表单

javascript - Onclick javascript使浏览器返回上一页?

android - 我正在寻找缺少 proguard.cfg 的 default.properties 文件

android - 如何逐步在后台使用前置摄像头拍摄照片?

user-interface - 用vue实现全局对话框

javascript - 如何在点击时切换 Font Awesome 图标?

javascript - 有没有办法检测页面上除特定区域之外的任何位置的点击?