I use a CompoundButton
in my app. I call a method named newsLetterUpdate()
in the onCheckedChange()
callback of CompoundButton.OnCheckedChangeListener
. I change the checked state of this CompoundButton
programmatically, but the onCheckedChange()
callback gets invoked. I want the onCheckedChange()
to be triggered only if I switch the checked state on the View
.
Please suggest me a solution to change the state of compound button without the onCheckedChange()
callback to be invoked.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
I have the similar issue. I had RadioGroup and when the user was opening the application I was setting the initial state and it was going in OnCheckedChangeListener and in my case was updating data in database which was very bad. I have solved it that in the OnCheckedChangedListener i was taking the button and setting in that method a OnClickListener:
With that when onCreate was called it was going in OnCheckedChangedListener but it wasn't going in OnClickListener. Only when the user will press the button it will go in OnClick and will execute in my case the startService. Hope this will help.
Method 4: Use a custom view
Sample XML usage
Now the idea is to call the method setCheckedProgrammatically in code. setChecked gets called by Android when users changes the state of the compund button
Note that I'm using a switch, which extends the compund button, you can use basically the same code on any other (checkbox, ...)
You can check if onCheckedChange event is from user pressing the button, if you check isPressing value like this:
Method 1 : if you want on
checkChangeListener
should only be called while use interacts with the view then implemenetonClickListener
putnewsLetterUpdate
method in that classMehtod 2 : unregister the listener before you programmaticaly change the checkbox and register again when you are done
Method 3 : use a boolean flag... set it when you change checkbox programmactically and set if false to when u are done... guard the code with that flag which you want to execute with user interaction only
So as per your requirement I believe that you need to execute the code inside
onCheckedChange()
call back only if the user initiates the action. Right?Then why are you using
onCheckedChange()
, you can useonClickListener()
instead to achieve your goal.Currently you have possibly written your code inside:
compundbutton.setOnCheckedChangeListener(your_current_listener);
Move that code from there to :
compundbutton.setOnClickListener(your_new_listener);
Then
onCheckedChange ()
listeners will not get invoked on setting checked state programiccally.compundbutton.setChecked(checked);
Hope it may help you..! :)