Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Can we extends OnClickListener another Class?

public class A extends Activity {
   public void onCreate(Bundle b){
      ~
       Button b1 = (Button) findViewById(R.id.btn);
       b1.setOnClickListener(new B());
      ~
   }
}

public class B extends OnClickListener{
    ~ do something public void onClick() etc..
}

İs it possible or is there an any way to do this?

like image 458
natrollus Avatar asked Jun 14 '26 05:06

natrollus


1 Answers

OnClickListener is an interface, so depending on what you want to do with it you can either use:

public interface MyClickListener extends OnClickListener {}

public class MyClickClass implements OnClickListener {}

Looking at your example, I assume that you want the second option:

public class MyClickClass implements OnClickListener {
    @Override
    public void onClick(View v) {
        Log.v("MyClickClass", "b1 clicked!");
    }
}

Use it like this: b1.setOnClickListener(new MyClickClass());

like image 126
Sam Avatar answered Jun 16 '26 17:06

Sam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!