ImageButton 在第一次点击时并不总是可点击

ImageButton not always clickable on the first click

我在布局中有多个 ImageButtons。图片资源为 32x32 像素。它们都具有相同的属性:

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/ibButton1"
                android:clickable="true"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:layout_weight="0"
                android:src="@drawable/not_selected"
                android:layout_gravity="center_vertical"
                android:background="@null"/>

在我的片段中,我正在做:

    final ImageButton button1 = (ImageButton)view.findViewById(R.id.ibButton1);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            isSelected = !isSelected;

            if (isSelected ) {
                button1.setImageResource(R.drawable.selected);
            }
            else {
                button1.setImageResource(R.drawable.not_selected);
            }
        }
    });

但是,大多数时候我需要点击按钮超过 5 次才能注册点击。

我需要增加图像尺寸还是有更好的方法来听点击?我应该改用 onClick 属性吗?

提供一些填充以增加 Tap 区域。

android:padding="10dp"

也许,图像的尺寸太小(如@Shoeb Siddique 所说),无法在图像上获得触摸事件。您可以增加图像的填充或大小以提高触摸图像的可能性。

在我看来,您不需要 ImageButton,而是需要带有状态的 ToggleButton。 http://developer.android.com/guide/topics/ui/controls/togglebutton.html

请删除 android:clickable="true" ,不需要,因为它是一个按钮,这意味着默认情况下为真。如果它是 TextView,则需要它。

提取带有触摸区域的图像我的意思是透明背景 例如:Give Image 是 86 * 70 px 所以当用户触摸到透明背景 86 * 70px 时你可以触发事件