Difference between OnClick() event and OnClickListener?

后端 未结 16 2428
醉话见心
醉话见心 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: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...
        }
    }
    

提交回复
热议问题