简体   繁体   English

DialogFragment onClick侦听器不适用于ImageButton

[英]DialogFragment onClick listener doesn't work with ImageButton

I think I tried everything that was provided here. 我想我尝试了这里提供的所有内容。 I have ImageButton in my activity and I can't set it to switch to another activity (Log.d shows that it's not even clicked). 我的活动中有ImageButton,我无法将其设置为切换到另一个活动(Log.d显示它甚至没有被点击)。

Here's my Dialog: 这是我的对话:

    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 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" />

I hope there's a way to solve it. 我希望有办法解决它。

I think there is No need for you to have an instanse of Dialog in your StarsActivity which is already extending from Dialog fragment. 我认为没有必要在StarsActivity中有一个Dialog实例,它已经从Dialog片段扩展。 The below code does everything that you want 以下代码可以完成您想要的所有操作

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;
        }
    }
}

Please consider renaming StarsActivity as StarsDialog or something else. 请考虑将StarsActivity重命名为StarsDialog或其他内容。

在你的Oncreate方法中添加以下行:

nextQuestion.setOnClickListener(this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM