【问题标题】:ImageView setOnClickListener Not Work (Java/Android)ImageView setOnClickListener 不工作 (Java/Android)
【发布时间】:2022-01-24 07:14:40
【问题描述】:

我在图像视图上的 Click Listener 遇到了一些问题
如果单击按钮
,我会尝试记录一些文本 但它根本不起作用

这是我的代码,希望你能帮助我:

ChatHeadService.java

ImageView closeButton = (ImageView) mChatHeadView.findViewById(R.id.close_btn);
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("myTag", "This is my message");
                //close the service and remove the chat head from the window
                stopSelf();
            }
        });

Layout_chat_head.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="65dp"
    android:id="@+id/chat_head_root"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!--Profile image for the chat head.-->
    <ImageView
        android:id="@+id/chat_head_profile_iv"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginTop="8dp"
        android:src="@drawable/icon_128"
        tools:ignore="ContentDescription" />

    <!--Close button-->
    <ImageView
        android:id="@+id/close_btn"
        android:layout_width="26dp"
        android:layout_height="26dp"
        android:layout_marginLeft="40dp"
        android:src="@drawable/close"
        tools:ignore="ContentDescription" />
</RelativeLayout>

[编辑] 这是布局的图像:

P/s:我在这里读过类似的问题:Android ImageView's onClickListener does not work

但这似乎不适合我。

【问题讨论】:

  • 您遇到的错误是什么?
  • 这不是错误。就是不行,setOnClickListener() 不能识别点击
  • 您确定日志显示正常吗?
  • 是的,我测试过,日志工作正常。
  • 您可以添加布局的屏幕截图吗?您没有为相对布局的子级定义任何对齐方式。

标签: java android xml kotlin


【解决方案1】:

我遇到了同样的问题。 ImageView 可能被其他视图隐藏/在背景中。使用一些海拔
android:elevation="3dp"

【讨论】:

  • 它对我不起作用 :(
  • ImageView[chat_head_profile_iv] 工作吗?
  • 不,它也不行
  • chat_head_profile_iv 我使用 setOnTouchListener 而不是 setOnClickListener。但它也不起作用
  • 对不起。不知道:(
【解决方案2】:

比我想象的要简单

我只是从这个改变:

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                LAYOUT_FLAG,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

到这里:

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                LAYOUT_FLAG,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, //<-- Right here :)
                PixelFormat.TRANSLUCENT);

【讨论】:

  • 您从未在原始帖子中发布此内容,因此您无法让任何人真正回答您。最好在您的帖子中包含所有相关细节,这样人们就可以真正重现问题
  • 好的,我认为解决这个问题不需要这些信息。不过,下次我会学习的。感谢您的评论
【解决方案3】:

在我看来,这不是 OnClickListener 问题。 只需将 stopSelf 的所有代码放入 Handler(Looper.getMainLooper()).run{} 那就试试吧。

【讨论】:

    【解决方案4】:

    尝试重新安装应用。检查 logcat 中的错误以允许操作机器人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-06
      相关资源
      最近更新 更多