Change Checkbox Value Without Triggering Oncheckchanged

Change Checkbox value without triggering onCheckChanged

No, you can't do it. The onCheckedChanged method is called directly from setChecked. What you can do is the following:

mCheck.setOnCheckedChangeListener (null);
mCheck.setChecked (false);
mCheck.setOnCheckedChangeListener (mListener);

See the source of CheckBox, and the implementation of setChecked:

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;
}
}

Change Checkbox value without raising event

You can derive from CheckBox class, override CheckBox.OnCheckedChanged Method and do not call the base class's OnCheckedChanged method and the event will not be fired. You can also have a property which indicates whether the event should be raised or not.

How can I distinguish whether Switch,Checkbox Value is changed by user or programmatically (including by retention)?

Answer 2:

A very simple answer:

Use on OnClickListener instead of OnCheckedChangeListener

    someCheckBox.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// you might keep a reference to the CheckBox to avoid this class cast
boolean checked = ((CheckBox)v).isChecked();
setSomeBoolean(checked);
}

});

Now you only pick up click events and don't have to worry about programmatic changes.


Answer 1:

I have created a wrapper class (see Decorator Pattern) which handles this problem in an encapsulated way:

public class BetterCheckBox extends CheckBox {
private CompoundButton.OnCheckedChangeListener myListener = null;
private CheckBox myCheckBox;

public BetterCheckBox(Context context) {
super(context);
}

public BetterCheckBox(Context context, CheckBox checkBox) {
this(context);
this.myCheckBox = checkBox;
}

// assorted constructors here...

@Override
public void setOnCheckedChangeListener(
CompoundButton.OnCheckedChangeListener listener){
if(listener != null) {
this.myListener = listener;
}
myCheckBox.setOnCheckedChangeListener(listener);
}

public void silentlySetChecked(boolean checked){
toggleListener(false);
myCheckBox.setChecked(checked);
toggleListener(true);
}

private void toggleListener(boolean on){
if(on) {
this.setOnCheckedChangeListener(myListener);
}
else {
this.setOnCheckedChangeListener(null);
}
}
}

CheckBox can still be declared the same in XML, but use this when initializing your GUI in code:

BetterCheckBox myCheckBox;

// later...
myCheckBox = new BetterCheckBox(context,
(CheckBox) view.findViewById(R.id.my_check_box));

If you want to set checked from code without triggering the listener, call myCheckBox.silentlySetChecked(someBoolean) instead of setChecked.

Android - programmatically change the state of a switch without triggering OnCheckChanged listener

Well, just before doing things in code with the switch you could just unregister the Listener, then do whatever you need to, and again register the listener.

Checkbox default value & OnCheckedChangeListener

The code of the CheckBox 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;
}
}

So basically you cannot use the method without firing events, unless you remove or disable the event handler before (or set them afterwards only).

android databinding: how to avoid onCheckedChanged triggered by programmatically

I faced the same problem and i used onCLick listener instead onCHeck
listener .That way the listener wont change the check state when it is set programatically.
In your problem you should try setting different check change listeners to your check boxes.

onCheckedChanged called automatically

It's weird all of us have this problem but not official Google answer to this simple problem.

The MOST simple it's to check:

buttonView.isPressed()

If true, means the user clicked the view.

No global variables are needed.

Laravel 7 - Update value of checkbox without form or button

I think you can use something like JQuery for this.

You can firstly add a class of toggle-class to your checkbox

<input class="active toggle-class" {{$teacher->grade == 1 ?'checked':''}} type="checkbox" name="grade">

And secondly, make sure that you have a way to keep track of who's doing the grades.

Add data-id="{{ $teacher->Identifier}}" on your checkbox

<input data-id="{{ $teacher->Identifier}}" class="active toggle-class" {{$teacher->grade == 1 ?'checked':''}} type="checkbox" name="grade">

In your web.php add a new route

Route::get('/path/to/updateGrader/status', [App\Http\Controllers\MyController::class, 'updateGraderStatus']);

Add the below code to where your checkbox is located

$('.toggle-class').on('change',function(){
let status = $(this).prop('checked') == true ? true : false;
let grader_id = $(this).data('id');
$.ajax({
type:'GET',
dataType:'json',
url:'/path/to/updateGrader/status', // add your earlier created route here
data:{'grade': status, 'grader_id': grader_id},
success: function(data){
console.log('success');
}
});
});

In your Controller, MyController in our case create a new function

public function updateGraderStatus(Request $request)
{
$grader = MyModel::find($request->grader_id);
$grader->grade = $request->grade;
$grader->save();
}

Please let me know if this helps you. I didn't test it out but I think this is how you would go about it.



Related Topics



Leave a reply



Submit