OnClickListener 不适用于可点击属性

发布于 2024-11-28 10:48:04 字数 986 浏览 5 评论 0原文

所以,我的问题是,当我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

缱绻入梦 2024-12-05 10:48:04

设置 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 your MyClass view is the parent of the RelativeLayout 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.

原来分手还会想你 2024-12-05 10:48:04

当你这样做时:android:clickable="true"你禁用了onClickListener(它不合逻辑,但就像那样......)。

因此,将其设置为 "false" 或从 XML 文件中删除此行;)

when you do this: android:clickable="true" you disable the onClickListener (its not logical but its like that..).

So set it to "false" or just remove this line from your XML file ;)

薄荷→糖丶微凉 2024-12-05 10:48:04

将 id 添加到您的布局中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/yourId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:clickable="true">
</RelativeLayout>

然后在 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);
        findViewById(R.id.yourId).setOnClickListener(this);
    }

    public void onClick(View v) {
        Log.v("TAG", "Hello");
    }
...
...
}

Add id to your layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/yourId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:clickable="true">
</RelativeLayout>

Then in java code:

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);
        findViewById(R.id.yourId).setOnClickListener(this);
    }

    public void onClick(View v) {
        Log.v("TAG", "Hello");
    }
...
...
}
溺渁∝ 2024-12-05 10:48:04
 ((RelativeLayout)findViewById(R.id.yourId)).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                //code area ...
                Log.v("TAG", "Hello");

                return true;
            } else{
                return false;
            }
        }
    });

您也可以使用这种方式。

 ((RelativeLayout)findViewById(R.id.yourId)).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                //code area ...
                Log.v("TAG", "Hello");

                return true;
            } else{
                return false;
            }
        }
    });

You can use this way too.

唐婉 2024-12-05 10:48:04

如果只有很少的元素需要执行操作,则可以在过滤后为每个元素设置 OnClickListener,而不是为整个类实现 OnClickListener。

TextView textLogin = findViewById(R.id.textLogin);

textLogin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Log.i("click", "textLoginClicked.");
    }
});

否则,您应该声明要设置 OnClickListener 的元素,例如,

textLogin.setOnClickListener(this);

然后您可以使用,

@Override
public void onClick(View view) {
    if (view.getId() == R.id.textLogin) {
        Log.i("Click", "Login clicked.");
    }
}

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.

TextView textLogin = findViewById(R.id.textLogin);

textLogin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Log.i("click", "textLoginClicked.");
    }
});

Else you should state that the elements you are going to set OnClickListener like,

textLogin.setOnClickListener(this);

Then you can use,

@Override
public void onClick(View view) {
    if (view.getId() == R.id.textLogin) {
        Log.i("Click", "Login clicked.");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文