By declaring that the class implements OnClickListener and declaring it yourself?

I apologize for my title, I am having problems with the correct formulation of the problem.

I saw that OnCLickListener implemented in two ways. The first is done by indicating that your class implements OnCLickListener . The second performs the task, declaring itself.

Why in the first option you can just put this as your argument to setOnCLickListener , but in the second you have to solve the problem of creating the OnCLickListener object yourself?

First:

 public class WidgetConfig extends Activity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.widgetconfig); Button b = (Button)findViewById(R.id.bwidgetconfig); b.setOnClickListener(this); } //onClick defined outside of the onCreate @Override public void onClick(View arg0) { // TODO Auto-generated method stub } 

The second:

 public class WidgetConfig extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.widgetconfig); Button b = (Button)findViewById(R.id.bwidgetconfig); b.setOnClickListener(bListener); } private Button bListener = new OnClickListener(){ b.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { //TO-DO } }); 
+4
source share
5 answers

In the first method, you entire class Activity implement the OnClickListener interface. You can set the OnClickListener each view to this and receive all click events in one way, where you can then filter them and act on them.

The second method uses an anonymous inner class that implements the interface method. Using this approach, you only receive events for this particular view.

In the first method, your entire class uses one single instance of OnClickListener , which is passed to all the views you want to listen for clicks.

The second method translates as:

 Button.OnClickListener anonymous_listener = new Button.OnClickListener() { ... }; button.setOnClickListener(anonymous_listener); 

That is, it dynamically creates and saves a new instance of OnClickListener when you use it.

+4
source

I usually do the first way because it saves the object. But if you need to implement a lot of listeners, then to make the code more organized and accurate, you can consider the second method.

+1
source

In Case 1, this represents an object of type Activity and OnClickListener . So, when you pass this to b.setOnClickListener (this), it represents an object of type 'OnClickListener'.

In case 2, you pass an anonymous object type OnClickListener . Since OnClickListener is an interface , you must define the onClick method inside an anonymous object.

+1
source

If your onClickListener needs to access most of the methods and member variables of your Activity class, then using the first approach, you can do this to simplify the code. It may also be preferable if you want to handle all onClick in the same way.

Otherwise, the second option should be done when you have many buttons (or other types of views that require click processing), and they should all be treated differently. In this case, it makes no sense to have an Activity21> onClick handle for one button (view), and the rest of them are handled by an individual onClickListener , since the code looks incompatible.

+1
source

Your WidgetConfig class is β€œactivity” because it extends the action. However, your WidgetConfig "is (also)" an OnClickListener because it implements the interface. Thus, when setOnClickListener() asks you to pass the click listener as an argument, you can pass " this " because " this " (which is WidgetConfig) is " OnClickListener .

With an anonymous inner class, you get setup and callback in one. You should read this, however, about the potential errors of using such classes: When exactly is it safe to leak for using (anonymous) inner classes?

+1
source

Source: https://habr.com/ru/post/1490415/


All Articles