Difference between OnClick() event and OnClickListener?

后端 未结 16 2341
醉话见心
醉话见心 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 12:57

    We use

        public void button_onClick_name(View v)
    {
    -------
    }
    

    to define a method out of the class. But To define a component Click event within a class, we use onclick listener.

    0 讨论(0)
  • 2020-12-01 12:57

    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 learn 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 12:58

    I'm not sure the question is clear. View.OnClickListener is an interface, which defines the onClick(View) method. If you have a class which intends to listen for clicks, you should both implement the interface (if not already extending a class that does), and implement this method too. You have to use both; they're not somehow alternatives.

    0 讨论(0)
  • 2020-12-01 12:59

    Consider "OnClickListener" as a guy who is waiting your user to click the button of your app. Then your guy will execute your method OnClick().

    You have to put an id to your button in your xml file, then give it a name in your MainActivity.java file. Then set a click listener to your guy. And add your onClick method. That's why onClick is bound to the interface View.OnClickListener : https://developer.android.com/reference/android/view/View.OnClickListener.html

    Example :

    Button myButton = (Button)findViewById(R.id.myButton);
    myButton.setOnClickListener(new View.OnClickListener(){
        @override
        public void onClick(View v) {
            // your method...
        }
    }
    
    0 讨论(0)
  • 2020-12-01 13:02

    You can add android:onClick="your_method" attribute in your XML.

    Example:

     <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Click"
      android:onClick="your_method"/>
    
    0 讨论(0)
  • 2020-12-01 13:03

    We use OnClick in xml and OnClickListner in java code . Both are use to perform a function.

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