OnClickListener不与点击属性工作 [英] OnClickListener doesn't work with clickable attribute

查看:232
本文介绍了OnClickListener不与点击属性工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的问题是, OnClickListener 当我设置不起作用机器人:可点击=真正的进入我的课。

So, my problem is that OnClickListener doesn't work when I set android:clickable="true" into my class.

这是 MyClass的 XML code:

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:

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");
    }
...
...
}

它工作正常,当我设置机器人:可点击为false。做什么我错了?

It works fine when I set android:clickable to false. What do I wrong?

推荐答案

设置一个 OnClickListener 将自动设定点击属性为true。你是显示的code是混乱的,但。我的理解是,你的 MyClass的视图是父 RelativeLayout的在XML文件中所示。

Setting an OnClickListener will automatically set the clickable property to true. The code you are showing is confusing though. My understanding is that your MyClass view is the parent of the RelativeLayout shown in the XML file.

如果是这样,孩子 RelativeLayout的将首先得到触摸事件(因为它是可点击的),但是不会跟他们做任何事情,因为它没有一个点击监听器。

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.

只是删除​​点击= TRUE 从XML。

Just remove clickable=true from your XML.

这篇关于OnClickListener不与点击属性工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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