开发者

How can I take listener methods out of their scope in Java?

开发者 https://www.devze.com 2023-04-07 04:20 出处:网络
public class SomeClass { public void SomeMethod() { GridView gv = new GridView(this); gv.setOnClickLi开发者_C百科stener(new GridView.OnClickListener() {
public class SomeClass {
    public void SomeMethod() {
        GridView gv = new GridView(this);
        gv.setOnClickLi开发者_C百科stener(new GridView.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
            }

        });
    }

    // Define onClick here and reference it somehow above?

}

How can I take the onClick method out of that block of code into the root of the class? Like you can do it on C#, for instance.


create the following inner class

class click implements OnClicklIstener
{
 public void onClick(View v) {
      // do your stuff
    }

}

and

gv.setOnClickListener(new click());


You can declare a Listener as any other class attribute:

public class HomeActivity extends Activity {

 public void SomeMethod() {
        GridView gv = new GridView(this);
        gv.setOnClickListener(gridClickedListener);
    }

 private GridView.OnClickListener gridClickedListener =  new GridView.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
            }

        });
}

Or implement the OnClickListener (or other) interfaces, but you will need to know which which view was clicked (switch inside the OnClick handler) if several views will report to this listener. I tend not to use this approach - it looks less readable for me.


Like this:

public class SomeClass {
    public void SomeMethod() {
        GridView gv = new GridView(this);
        gv.setOnClickListener(new GridView.OnClickListener() {

            @Override
            public void onClick(View view) {
                SomeClass.this.onClick(view);
            }

        });
    }

    public void onClick(View view) {
        // implement me please
    }

}


Easy, just implement the OnClickListener interface.

public class SomeClass extends Activity implements OnClickListener 

 // Implement the OnClickListener callback
    public void onClick(View v) {
      // check the view type : Use View v to check which view is this called for using getId () 
    }
    ...

Note that you have to check which view has been clicked before doing your handling.


Won't this work?

public class SomeClass implements GridView.OnClickListener {
    public void SomeMethod() {
        GridView gv = new GridView(this);
        gv.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
    }
}


public class SomeClass extends Activity implements View.OnClickListener 
{
GridView gv;
 public void SomeMethod() {
        gv = new GridView(this);
        gv.setOnClickListener(this); 
     }

   @Override
   public void onClick(View v) {
   if(v==gv)
   {
      // do something when the button is clicked
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号