【问题标题】:DialogFragment onClick listener doesn't work with ImageButtonDialogFragment onClick 侦听器不适用于 ImageButton
【发布时间】:2015-04-18 09:32:07
【问题描述】:

我想我尝试了这里提供的所有内容。我的活动中有 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;
        }
    }
}

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

希望有办法解决。

【问题讨论】:

  • 我无法理解您的问题,请您澄清一下您的问题。

标签: android dialog onclick onclicklistener dialogfragment


【解决方案1】:

我认为你不需要在你的 StarsActivity 中有一个 Dialog 实例,它已经从 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 或其他名称。

【讨论】:

  • 谢谢,但问题是我需要我的对话框具有以下功能:没有标题、透明背景、有父级宽度并且使用 onCreateView 我无法让它工作。
  • 我设法让它与保持 onCreateDialog() 方法一起工作。谢谢!
【解决方案2】:

在你的 Oncreate 方法中添加这一行:

nextQuestion.setOnClickListener(this);

【讨论】:

  • 您不能在onCreate 中引用DialogFragment 上的布局ID,片段的视图设置在onCreateDialog 中,只有这样您才能引用ID。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-02
  • 1970-01-01
  • 2014-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多