Android setOnclicklistener parameter

后端 未结 6 1526
忘了有多久
忘了有多久 2021-02-06 07:58

I\'m a beginner to android, while setting onclick listener to a button, what does the parameter passed mean:

 btn1.setOnClickListener(new OnClickListener() {

         


        
相关标签:
6条回答
  • 2021-02-06 07:58

    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 object
    private OnClickListener btnClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
    
        }
    };
    
    //passing listener object to button
    btn1.setOnClickListener(btnClick);
    

    to do it without using Anonymous class:

    public class YourActivity extends Activity implements OnClickListener {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
           Button b = new Button(this);
    
           //setting listener to button
           b.setOnClickListener(this);
        }
    
        @Override
        public void onClick(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.

    0 讨论(0)
  • 2021-02-06 08:04

    Interface definition for a callback to be invoked when a view is clicked.

    abstract void onClick(View v) Called when a view has been clicked.

    0 讨论(0)
  • 2021-02-06 08:07

    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

    0 讨论(0)
  • 2021-02-06 08:12

    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:

    OnClickListener l = new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
        }
    });
    btn1.setOnClickListener(l);
    

    and to use a separate class, you can do it like this:

    btn1.setOnClickListener(new MyOwnListener());
    
    // ...
    
    public class MyOwnListener implements OnClickListener
    {
        // ...
    
        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
        }
    }
    
    0 讨论(0)
  • 2021-02-06 08:14

    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:

    class OnButtonClick implements OnClickListener{
    
        public void onClick(View v) {
            // TODO Auto-generated method stub
        }
    
    }
    

    Then you can use:

    OnButtonClick obc = new OnButtonClick();
    textView.setOnClickListener(obc);
    

    This is more Java than android, Read about Interfaces and Inner Classes

    0 讨论(0)
  • 2021-02-06 08:24

    Read up on anonymous classes in Java.

    0 讨论(0)
提交回复
热议问题