【问题标题】:Android ImageButton click not working sometimesAndroid ImageButton点击有时不起作用
【发布时间】:2016-05-04 08:04:13
【问题描述】:

我在我的 xml 中使用 ImageButton,如下所示:

<FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        >
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton_home"
            android:src="@drawable/home_icon"
            android:background="@android:color/transparent"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:focusableInTouchMode="false"
            android:focusable="false"
            android:clickable="true"
            />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton_back"
            android:src="@drawable/back_icon"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:background="@android:color/transparent"
            android:visibility="gone"
            android:focusableInTouchMode="false"
            android:focusable="false"
            android:clickable="true"
            />

    </FrameLayout>

一次只能看到一个图像按钮。 我正在捕获我的 java 代码中的点击,如下所示:

  imageButtonHome.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent homeIntent = new Intent(SettingsActivity.this, MainActivity.class);
            startActivity(homeIntent);
        }
    });

这有时可以正常工作,但并非总是如此。 每当我单击图像按钮时,我都会在我的 logcat 中看到这一行:

D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN

即使我的 ImageButton 单击操作未执行,此行也会始终显示。

但是当我的 ImageButton 点击​​工作正常时,另一行加起来是日志:

 D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
 D/AbsListView: Get MotionRecognitionManager

我希望我的图像按钮每次都能正常工作。请帮忙。

【问题讨论】:

  • 你为什么使用框架布局??
  • 因为我需要将这两个按钮放在同一位置,但一次只需要显示一个。
  • 你可以使用相对布局
  • 但我不认为,这是由于框架布局。我也在其他 XML 中使用 ImageButton,它们没有像这样包裹在框架布局中,但它们仍然显示相同的行为。
  • 第二个按钮的可见性消失了。你会怎么看

标签: android onclicklistener imagebutton


【解决方案1】:

为您的 ImageButtons 提供一些填充以捕获触摸事件。

【讨论】:

  • 是的,但这会使它们变小...我不想要那个
  • 填充不会使按钮变小。请尝试。
  • @FebiMathew 我有同样的问题
【解决方案2】:

在 ImageButton 上使用 setOnClickListener(...)

【讨论】:

    【解决方案3】:

    尝试使用 LinearLayout 而不是 FrameLayout。

    FrameLayout 被认为只包含 1 个孩子。

    【讨论】:

      【解决方案4】:

      替换它并尝试。

      imageButtonHome.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              Intent homeIntent = new Intent(SettingsActivity.this, MainActivity.class);
              startActivity(homeIntent);
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-18
        • 1970-01-01
        • 1970-01-01
        • 2016-07-26
        相关资源
        最近更新 更多