Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnCheckedChangeListener or OnClickListener with if statement for CheckBox's? What is the difference in functionality?

Hello I'm creating an android application that uses checkbox's, i'm wondering if it is better to use an OnCheckedChangeListener to do something when the state of the checkbox is changed or if it would be better to use an OnClickListener with an if statement inside it that is executed everytime the checkbox is checked or unchecked? Thanks

like image 668
Paul Alexander Avatar asked Mar 21 '14 16:03

Paul Alexander


People also ask

What is view OnClickListener ()?

In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.

How do I use OnClickListener?

If you have more than one button click event, you can use switch case to identify which button is clicked. Link the button from the XML by calling findViewById() method and set the onClick listener by using setOnClickListener() method. setOnClickListener takes an OnClickListener object as the parameter.

Which class contains the onClick () method?

To answer your question directly, the onClickListere is an anonymous class that defines the onClick method, which will handle button events.

What is view V in Android Studio?

The View v is the object of your xml file which is referred in your onCreate method. To refer any component from xml you have to use v to gets its id of the component. Condition. You have give id to the component in xml if you want to use onClick in your class file.


1 Answers

With OnCheckedChangeListener you receive an event whenever the checked status changes, even when done in code by using .setChecked().

Depending on what you are doing this can lead to unexpected behavior (for example when you have a checkbox in a listview, the view is recycled and the checkbox state is modified programmatically, it looks exactly the same way as if the user had clicked it).

Therefore, when you are writing code that is supposed to react to a user who clicked the checkbox you should use OnClickListener.

like image 157
akirk Avatar answered Oct 16 '22 19:10

akirk