开发者

Android: How to only hear when my custom class is clicked

when I make a custom view class and add a clickListener it fires anywhere on the screen I click, even where the custom view is not. If I use the same code with a button from a layout it only fires when I click the button not anywhere on screen. Any ideas how to just only listen for when my custom class is directly clicked?

button only fire when pressed

   Button b = (Button) findViewById(R.id.button1); 
   b.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
      Log.d(DEBUG_TAG, "button clicked");
   }
});

stroke object fires when you press anywhere on screen, even outside of stroke's bounding box

    Stroke stroke = new Stroke(this);
    mainLayout.addView(stroke);
    stroke.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
      // fires on every scre开发者_如何转开发en click :>(
      Log.d(Main.DEBUG_TAG, this.toString()+"shape clicked");
   }
    });


I think your custom view just fills the whole screen. That's why it reacts on every click on the screen. You need to make it smaller and everything will work fine.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜