2013-10-14 26 views
1

我试图在我的片段类中收到复选框选项,但是我无法使用我在活动中使用的方式(从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; 

    } 
} 
+0

实现在碎片中找到? – gunar

回答

3
@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。

1

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

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

只需使用您的自定义监听

CompoundButton.OnCheckedChangeListener

任何其他视图然后活动内。下面的代码示例

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; 
     } 
    } 
}; 
} 
0
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个!