Difference between OnClick() event and OnClickListener?

后端 未结 16 2342
醉话见心
醉话见心 2020-12-01 12:13

I\'m always using onclick() event in most of my projects. But, I read about OnClickListener(). Can anyone tell what\'s the difference between these

相关标签:
16条回答
  • 2020-12-01 13:03

    The main diffrence between onclick() and setOnClicklisner() is discribed as follow:

    1. onclick()
      Is a attribute in xml. when a button is clicked onclick method is called suppose you have three button in layout you can add only one function of onclick() and when any of one button will clicked onclick() will called

    2. setOnClicklistner()
      Suppose you have three button in layout you want to perform different action from them. Then you should use setonClicklistner() method on each of button to give different method for them

    0 讨论(0)
  • 2020-12-01 13:06

    There are a couple reasons why you might want to programmatically set an OnClickListener. The first is if you ever want to change the behavior of your button while your app is running. You can point your button at another method entirely, or just disable the button by setting an OnClickListener that doesn't do anything.

    When you define a listener using the onClick attribute, the view looks for a method with that name only in its host activity. Programmatically setting an OnClickListener allows you to control a button's behavior from somewhere other than its host activity. This will become very relevant when we talk about Fragments, which are basically mini activities, allowing you to build reusable collections of views with their own lifecycle, which can then be assembled into activities. Fragments always need to use OnClickListeners to control their buttons, since they're not Activities, and won't be searched for listeners defined in onClick.

    0 讨论(0)
  • 2020-12-01 13:10

    Here is the simple terminology If u are at home and U want to call someone..u can call directly and they can listen u. (use onclick). But if u are outside and u want to Call someone at home u need to use either phone or Internet.(need to use onclicklistener). In Android everything starts from home, I.e. main_activity This is the way android eases yr work ; when u have one activity u don't have to attach a listener, create object, and define it. Just use onClick. Onclicklistener are generally used in Fragments. So Keep Coding.

    0 讨论(0)
  • 2020-12-01 13:15

    OnClickListener is the interface you need to implement and can be set to a view in java code.

    Lately android added a xml attribute to views called android:onclick, that can be used to handle clicks directly in the view's activity without need to implement any interface.

    Both function the same way, just that one gets set through java code and the other through xml code.

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