onClickListener没有被解雇

时间:2016-02-29 12:44:55

标签: android

我试图从自定义视图触发onClick事件但是onClickListener没有被解雇

@Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    mRootView = inflater.inflate(R.layout.myorders_fragment, container, false);
    linear = (LinearLayout) mRootView.findViewById(R.id.linear);
    radioGroup = (RadioGroup) mRootView.findViewById(R.id.segment_text);
    pending = (RadioButton) mRootView.findViewById(R.id.button_one);
    completed = (RadioButton) mRootView.findViewById(R.id.button_two);

    //  radioGroup.setOnCheckedChangeListener(this);
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            if (group == radioGroup) {
                if (checkedId == R.id.button_one) {

                    for (int i =0 ;i<=3;i++) {
                        mChildView = inflater.inflate(R.layout.pending_orders_fragment, linear);

                       mChildView.setClickable(true);
                        mChildView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(getActivity().getApplicationContext(),"sdf",Toast.LENGTH_SHORT).show();
                            }
                        });


                    }

                } else if (checkedId == R.id.button_two) {

                    Toast.makeText(getActivity().getApplicationContext(),"completed",Toast.LENGTH_SHORT).show();
                 //   mChildView = inflater.inflate(R.layout.pending_orders_fragment, linear);

                }
            }
        }
    });
    return mRootView;
}

子视图必须是可点击的,它应该触发onClickListener。为什么onClickListener没有被解雇?或者我应该尝试实现onTouchListener?

2 个答案:

答案 0 :(得分:3)

您的childViews没有父级,这就是OnClick不是callend的原因。您只是给它们充气,但不会将它们添加到父级。如果孩子不在,请与addView上的LinearLayout联系。getChildAt否则,请致电LinearLayout上的LinearLayout,而不是在let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"

中已有let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "MMM dd, yyyy"

答案 1 :(得分:1)

试试这个并告诉我是否有效:

mChildView
.setOnFocusChangeListener(new View.OnFocusChangeListener() {
      public void onFocusChange(View v, boolean hasFocus) {
           if (hasFocus) {
                v.performClick();
             }
         }
  });