OnClickListener 不适用于可点击属性
所以,我的问题是,当我将 android:clickable="true"
设置到我的类中时,OnClickListener
不起作用。
这是 MyClass
xml 代码:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:clickable="true">
...
...
</RelativeLayout>
MyClass.java:
public class MyClass extends RelativeLayout implements OnClickListener {
public MyClass(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.book_item, this);
setOnClickListener(this);
}
public void onClick(View v) {
Log.v("TAG", "Hello");
}
...
...
}
当我将 android:clickable
设置为 false 时,它工作正常。我有什么错吗?
So, my problem is that OnClickListener
doesn't work when I set android:clickable="true"
into my class.
This is MyClass
xml code:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:clickable="true">
...
...
</RelativeLayout>
MyClass.java:
public class MyClass extends RelativeLayout implements OnClickListener {
public MyClass(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.book_item, this);
setOnClickListener(this);
}
public void onClick(View v) {
Log.v("TAG", "Hello");
}
...
...
}
It works fine when I set android:clickable
to false. What do I wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
设置
OnClickListener
会自动将 clickable 属性设置为 true。不过,您显示的代码令人困惑。我的理解是,您的MyClass
视图是 XML 文件中显示的RelativeLayout
的父视图。如果是这样,子
RelativeLayout
将首先获取触摸事件(因为它是可点击的),但不会对它们执行任何操作,因为它没有点击侦听器。只需从 XML 中删除
clickable=true
即可。Setting an
OnClickListener
will automatically set the clickable property to true. The code you are showing is confusing though. My understanding is that yourMyClass
view is the parent of theRelativeLayout
shown in the XML file.If so, the child
RelativeLayout
will get the touch events first (since it's clickable) but won't do anything with them since it doesn't have a click listener.Just remove
clickable=true
from your XML.当你这样做时:
android:clickable="true"
你禁用了onClickListener
(它不合逻辑,但就像那样......)。因此,将其设置为
"false"
或从 XML 文件中删除此行;)when you do this:
android:clickable="true"
you disable theonClickListener
(its not logical but its like that..).So set it to
"false"
or just remove this line from your XML file ;)将 id 添加到您的布局中:
然后在 java 代码中:
Add id to your layout:
Then in java code:
您也可以使用这种方式。
You can use this way too.
如果只有很少的元素需要执行操作,则可以在过滤后为每个元素设置 OnClickListener,而不是为整个类实现 OnClickListener。
否则,您应该声明要设置 OnClickListener 的元素,例如,
然后您可以使用,
Instead of implementing OnClickListener to the whole class, you can set OnClickListener to each of the elements after filtering them if there are only few elements to do actions.
Else you should state that the elements you are going to set OnClickListener like,
Then you can use,