How to pass parameters to OnClickListener?

后端 未结 9 2251
南旧
南旧 2020-11-29 20:59

How can i pass parameter to an OnClickListener() ?

Got my Listener:

   OnClickListener myListener = new OnClickListener()
   {

     @Override
     p         


        
9条回答
  •  Happy的楠姐
    2020-11-29 21:40

    Another solution may be to not write the code directly inside onClick() method, alternatively, write the code in a separate method, and then call that method in onClick().

    Example:

    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        function();
       }
    });
    
    private void function() {
     //your code here
    }
    

提交回复
热议问题