Android setOnclicklistener parameter

后端 未结 6 1517
忘了有多久
忘了有多久 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 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

提交回复
热议问题