Android:仅使自定义视图的某些部分可点击 [英] Android: Only make certain part of custom view be clickable

查看:139
本文介绍了Android:仅使自定义视图的某些部分可点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义视图,假设它看起来像这样:

我希望我的自定义视图响应onClicks,但是要注意的是,我希望它仅响应红色部分/圆圈上的单击.不是整个视图.

是否可以使上面的文本和灰色部分不可点击?

谢谢.

解决方案

不幸的是,卡森(Carson)给出的答案与我所寻找的不完全相同,因为我的例子只是一个简单的例子,有时现实会更加困难,并且检查触摸位置会很麻烦(将自定义视图中的多个视图/形状想象为单击位置).

我在自定义视图中所做的是通过id在自定义视图的元素上进行查找视图.然后,我对要被单击的每个元素执行了setOnClickListener(this)而不是对整个视图本身进行了setOnClickListener(this),所以mCircle.setOnClickListener(this);mInput.setOnClickListener(this);然后对自定义视图执行了implements View.OnClickListener来处理操作.

I have a custom view, assume it looks like this:

I would like for my custom view to respond to the onClicks, however the catch is that I would like it to respond to the clicks ONLY on the red portion/circle. Not the whole view.

Is it possible to make to make the text above and the grey portion not clickable?

Thank you.

解决方案

Unfortunately the answer Carson posted was not exactly what I was looking for as my example was only a simple one, with the reality sometimes it being a lot more difficult and checking the touch locations would be convoluted (imagine multiple views/shapes within the custom view being the click locations).

What I did was in the custom view do a find view by id on the elements of the custom view. Then I did setOnClickListener(this) on each element that I would like to be clicked able rather than on the whole view itself, so mCircle.setOnClickListener(this); and mInput.setOnClickListener(this);, then did implements View.OnClickListener for the custom view to handle the actions.

这篇关于Android:仅使自定义视图的某些部分可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆