The default value for the checkbox and OnCheckedChangeListener

This is not a problem, but I'm trying to find the right way to do this.

I have the following situation:

public class SettingsDialogFragment extends DialogFragment implements OnCheckedChangeListener { ... @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.settings, container); ... CheckBox lBox1 = (CheckBox)view.findViewById(R.id.checkBox1); lBox1.setOnCheckedChangeListener(this); lBox1.setChecked(true); return view; } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { .... } 

My "problem" is that by calling setChecked(true) , onCheckChanged is already firing. I assume that when I inflate the layout, the CheckBox initialized with a false parameter, and I change it to true, this is the CheckedChanged event.

I could, of course, reorder and assign a listener after I set the initial value, but is there a way to inflate the layout by somehow passing the initial values ​​to the different components? They are dynamic, so I can’t fix the values ​​to a specific value in settings.xml

Greetings

+4
source share
6 answers

CheckBox code looks something like this:

 public void setChecked(boolean checked) { if (mChecked != checked) { mChecked = checked; refreshDrawableState(); // Avoid infinite recursions if setChecked() is called from a listener if (mBroadcasting) { return; } mBroadcasting = true; if (mOnCheckedChangeListener != null) { mOnCheckedChangeListener.onCheckedChanged(this, mChecked); } if (mOnCheckedChangeWidgetListener != null) { mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked); } mBroadcasting = false; } } 

Thus, you cannot use the method without triggering events, unless you delete or disable the event handler earlier (or install them only after that).

+3
source

The above suggestion is good, but this problem still exists in the ListView being checked. I solved this as follows: disconnect the listener, set the check state, and then install the listener again. Here is a helper function:

 private void checkCheckBox(CheckBox checkBox, boolean checked) { checkBox.setOnCheckedChangeListener(null); checkBox.setChecked(checked); checkBox.setOnCheckedChangeListener(this); } 
+6
source

You answered your question, setChecked(true) calls OnCheckedChangeListener .

A simple fix is ​​to add android:checked="true" to your CheckBox XML declaration and omit the setChecked(true) call.

+3
source

If you just want to set the initial values, your first sentence is likely to be the best: just register listeners after you initialize everything.

0
source

The solution is OnTouchListener . By default, you do not set OnCheckedChangeListener. You need to do the following:

 **declared in the class object declaration**: OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { **have fun** } }; **used to the point you would have set the OnCheckedChangeListener** anyView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { anyUsableView.setOnCheckedChangeListener(onCheckedChangeListener); return false; } }); 

Returning false to OnTouchListener is very important, otherwise the view will no longer work.

Tip. This solution can be used with any listener convenient for it (I use it with the Switch widget)

0
source

I think the solution could be the setChecked () function, which separates the listener before setting the marked value.

 public void setCheckedNoEvent(boolean checked) { if (onCheckedChangeListener == null) { switcher.setChecked(checked); } else { switcher.setOnCheckedChangeListener(null); switcher.setChecked(checked); switcher.setOnCheckedChangeListener(OnCheckedChangeListener); } } 
0
source

All Articles