【问题标题】:onClick not working inside of fragmentonClick 在片段内部不起作用
【发布时间】:2013-07-01 08:32:30
【问题描述】:

由于某种原因,onclick 侦听器没有响应以下片段中的点击事件,有什么想法吗?

public class TestFragment  extends Fragment{

    private TextView textViewOne;
    private RadioButton radioButtonOne;
    private RadioButton radioButtonTwo;
    private RadioButton radioButtonThree;
    private Spinner spinnerOne;
    private RadioGroup radioGroupOne;
            String buttonSelected;
            Activity activity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        activity = getActivity();


        View result=inflater.inflate(R.layout.tank_layout7, container, false);

         textViewOne = (TextView) result.findViewById(R.id.textView1);
         ratioButtonOne = (RadioButton) result.findViewById(R.id.radioButton1);
         ratioButtonTwo = (RadioButton) result.findViewById(R.id.radioButton2);
         ratioButtonThree = (RadioButton) result.findViewById(R.id.radioButton3);
         spinnerOne = (Spinner) result.findViewById(R.id.spinner1);
         radioGroupOne = (RadioGroup) result.findViewById(R.id.radio_group1);

        radioGroupOne.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                int checkedRadioButton = radioGroupOne.getCheckedRadioButtonId();
                switch(checkedRadioButton){
                 case R.id.radioButton1:
                     buttonSelected = "button one selected";

                  break;
                 case R.id.radioButton2:
                     buttonSelected = "button two selected";

                 break;      
                 case R.id.radioButton3:
                     buttonSelected = "button three selected";

                 break;
                 default:

                }
                Toast.makeText(activity, "radio button selected " + buttonSelected, Toast.LENGTH_SHORT).show();
            }
            });

        return(result);

    }

}

【问题讨论】:

    标签: android android-fragments onclicklistener


    【解决方案1】:

    RadioGroup 上使用OnCheckedChangeListener,而不是OnClickListener

    radioGroupOne.setOnCheckedChangeListener(new OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch(checkedId){
                // Your code    
            }   
        }
    });
    

    http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html

    【讨论】:

      猜你喜欢
      • 2014-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-08
      • 2020-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多