Android ImageButton 点击有时候不起作用

4

我在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

我想让我的图像按钮每次都能正常工作。请帮帮我。

你为什么要使用帧布局? - Vivek Mishra
因为我需要将这两个按钮放在同一位置,但每次只能显示一个。 - pratiti-systematix
你可以使用相对布局。 - Vivek Mishra
但我认为这不是由于框架布局。我在其他XML中也使用了ImageButton,它们没有像这样包含在framelayout中,但它们仍然显示相同的行为。 - pratiti-systematix
你的可见性已经消失了,第二个按钮怎么能看得到呢? - Vivek Mishra
需要时我将使其可见。 - pratiti-systematix
4个回答

8
为了捕捉触摸事件,给您的ImageButton添加一些填充。

是的,但那会让它们变小...我不想要那个。 - pratiti-systematix
填充不会使按钮变小。请尝试。 - Febi M Felix
@FebiMathew 我遇到了同样的问题。 - Wahdat Jan

1
在 ImageButton 上使用 setOnClickListener(...)。

-1

尝试使用LinearLayout而不是FrameLayout。

FrameLayout被认为只适用于包含一个子元素。


-2

请尝试替换此处内容。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接