Android Setonclicklistener Parameter
Solution 1:
OnClickListener is an interface and by using new OnClickListener()
as parameter for btn1.setOnClickListener it is actually creating an Anonymous Inner Class which implements OnClickListener. And the method onClick must need to be declared since its an abstract method inside that interface class. Any code you write inside onClick will be executed when the button is pressed.
Update
to do it using Anonymous inner class in an object:
//declaring OnClickListener as an objectprivateOnClickListenerbtnClick=newOnClickListener() {
@OverridepublicvoidonClick(View v) {
// TODO Auto-generated method stub
}
};
//passing listener object to button
btn1.setOnClickListener(btnClick);
to do it without using Anonymous class:
publicclassYourActivityextendsActivityimplementsOnClickListener {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
Buttonb=newButton(this);
//setting listener to button
b.setOnClickListener(this);
}
@OverridepublicvoidonClick(View v) {
// TODO Auto-generated method stub
}
}
The only difference between these approaches is, if your button contains click event code which is supposed to be valid/available for that button only, then you may use inner class as you are doing in your code (because its easy to do right away). However if there are multiple buttons which require same code to be executed on onClick event then you may define listener as an object and pass it to them.
Solution 2:
To instantiate an object of an interface or an abstract class, you need to override all of its not implemented methods (abstract methods). You can override the abstract methods by either using anonymous class or defining a class that extends the abstract class/implements the interface and override the abstract methods. However, to make it clearer, you can do it like this:
OnClickListenerl=newOnClickListener()
{
@OverridepublicvoidonClick(View v)
{
// TODO Auto-generated method stub
}
});
btn1.setOnClickListener(l);
and to use a separate class, you can do it like this:
btn1.setOnClickListener(newMyOwnListener());
// ...publicclassMyOwnListenerimplementsOnClickListener
{
// ...@OverridepublicvoidonClick(View v)
{
// TODO Auto-generated method stub
}
}
Solution 3:
OnClickListener()
is an interface. With the new keyword you are declaring a new object of an anonymous inner class that will implements the interface. That s base question for
java
than android. You should consider reading about
java interface
Solution 4:
Read up on anonymous classes in Java.
Solution 5:
OnClickListener
is an interface. It means if you set it inline you have created an anonymous class which is just implimenting the interface inside the set method.
If you wanted to create an OnClick
class. You would do something like this:
classOnButtonClickimplementsOnClickListener{
publicvoidonClick(View v) {
// TODO Auto-generated method stub
}
}
Then you can use:
OnButtonClickobc=newOnButtonClick();
textView.setOnClickListener(obc);
This is more Java than android, Read about Interfaces and Inner Classes
Post a Comment for "Android Setonclicklistener Parameter"