【问题标题】:Checkbox inside the Fragment - capturing checks/unchecks片段内的复选框 - 捕获选中/取消选中
【发布时间】:2013-10-21 20:52:14
【问题描述】:

我正在尝试在 Fragment 类中接收复选框选项,但是我无法使用我在活动中使用的方式(从 layout.xml 调用 android:onClick="onCheckboxClicked" 方法),因为我得到了“方法 onCheckboxClicked not found” 每次调用此方法时。

public void onCheckboxClicked(View view) {
    // Is the view now checked?
    boolean checked = ((CheckBox) view).isChecked();

    // Check which checkbox was clicked
    switch(view.getId()) {
        case (R.id.checkBox_wifi):
            if (checked)    {
                editor.putBoolean("wifi_on_off", true);
            }
            else    {
                editor.putBoolean("wifi_on_off", false);
            }
            break;

    }

    editor.apply();
}

所以,我已经实现了 View.OnClickListener,但这并没有得到任何复选框选中/取消选中:

 @Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.checkBox_wifi:
            boolean checked = ((CheckBox) view).isChecked();

            if (checked) {
                editor.putBoolean("wifi_on_off", true);
            } else {
                editor.putBoolean("wifi_on_off", false);
            }

            editor.apply();
            break;

    }
}  

【问题讨论】:

  • 实现在fragment中找到?

标签: android checkbox android-fragments


【解决方案1】:
    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragmentlayout, container,false);

    CheckBox checkboxvariable=(CheckBox)rootView.findViewById(R.id.checkboxid);

    checkboxvariable.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


            if(v.isChecked()){ //do this}
            else { //do this }

        }
    });
    return rootView;
}

checkboxvariable.setOnClickListener(this.getActivity());

在onClick里面你可以得到资源id,R.id.checkboxid。

【讨论】:

    【解决方案2】:

    任何onClick XML 属性将意味着必须在活动中找到方法,而不是在片段中,而不是在视图中。所以在我看来,你有两个选择:

    1. 在 Activity 中添加方法,并在实现中尝试查找片段,如果找到,则将调用委托给片段。
    2. 删除 XML onClick 并依赖您在 Java 代码中定义的 OnClickListeners。这个话题已经在at least this thread 讨论过。

    【讨论】:

      【解决方案3】:

      只需使用您的自定义侦听器

      CompoundButton.OnCheckedChangeListener

      在任何其他视图中,然后是 Activity。下面的代码示例

      public class MyFragment extends Fragment{
      
        public View onCreateView(...){
          ...
          CheckBox cbFilter = (CheckBox) rootView.findViewById(R.id.chb_rt_is_filter);
          cbFilter.setOnCheckedChangeListener(myCheckboxListener);
          return rootView;
        }
      
         private CompoundButton.OnCheckedChangeListener myCheckboxListener = new CompoundButton.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
              switch (buttonView.getId()){
                  case R.id.chb_rt_is_filter:
                      break;
                  default:
                      break;
              }
          }
      };
      }
      

      【讨论】:

        【解决方案4】:
        public class MyFragment extends Fragment {
        
            CheckBox myCheckBox;
        
            @Override
            public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle saveInstanceState) {
                rootView = layoutInflater.inflate(R.layout.your_fragment_layout, container, false);
                ...
                ...
                initViews();
                return rootView;
            }
        
            private void initViews() {
                myCheckBox = (CheckBox) rootView.findViewById(R.id.my_check_box);
                myCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                        if (isChecked)
                            Log.d("TAG=>isChecked", "True");//replace your own stuffs here
                        else
                            Log.d("TAG=>isChecked", "false");//replace your own stuffs here
                    }
                });
            }
        }
        

        您可以为 n 个复选框设置此侦听器!!!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-01-28
          • 2013-11-04
          • 1970-01-01
          • 1970-01-01
          • 2013-11-05
          相关资源
          最近更新 更多