2013-10-14 43 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個!