android - 自定义 View 不响应触摸

标签 android android-layout android-widget custom-controls touch

我创建了一个自定义 View ,它应该在按下、突出显示或禁用时更改它的背景图像。应用程序运行但按钮不会更改它的背景。

这是我的代码:

public class CustomImageButton extends View {

public CustomImageButton(Context context) {
super(context);
setFocusable(true);
setClickable(true);
}

public CustomImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
setFocusable(true);
setClickable(true);
}
public CustomImageButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFocusable(true);
setClickable(true);
}

protected Drawable background = super.getBackground();


@Override
public void setBackgroundDrawable(Drawable d) {
// Replace the original background drawable (e.g. image) with a LayerDrawable that
// contains the original drawable slightly edited.

CustomImageButtonBackgroundDrawable layer = new CustomImageButtonBackgroundDrawable(d);
super.setBackgroundDrawable(layer);
}

public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 int drawableWidth = super.getBackground().getMinimumWidth();
 int drawableHeight = super.getBackground().getMinimumHeight();
 setMeasuredDimension(drawableWidth, drawableHeight);
}

protected class CustomImageButtonBackgroundDrawable extends LayerDrawable { 

    protected Drawable lowerlayer;
    protected Drawable _highlightedDrawable;

    protected int _disabledAlpha = 100;
    protected Drawable _pressedDrawable;


    public CustomImageButtonBackgroundDrawable(Drawable d) {
          super(new Drawable[] { d });
    }

    @Override
    protected boolean onStateChange(int[] states) {
      boolean enabled = false;
      boolean highlighted = false;
      boolean pressed = false;

      for (int state : states) {
        if (state == android.R.attr.state_enabled)
            enabled = true;
        else if (state == android.R.attr.state_selected)
            highlighted = true;
        else if (state == android.R.attr.state_pressed)
            pressed = true;
      }

      mutate();
      if (enabled && highlighted) {
        ColorFilter colourFilter = new LightingColorFilter(Color.YELLOW, 1);
        ScaleDrawable resizedImage = new ScaleDrawable(background, 0, 1.25f, 1.25f);

        lowerlayer = resizedImage.getDrawable();
        lowerlayer.setColorFilter(colourFilter);

        Drawable[] aD = new Drawable[2];
        aD[0] = lowerlayer;
        aD[1] = background;
        LayerDrawable _highlightedDrawable = new LayerDrawable(aD);

        setBackgroundDrawable(_highlightedDrawable); // buttons need transparent backgrounds

      } else if (!enabled) {
        setColorFilter(null);
        setAlpha(_disabledAlpha);

      } else if (enabled && pressed){
        ScaleDrawable smaller = new ScaleDrawable(background, 0, 0.75f, 0.75f);

        setBackgroundDrawable(smaller.getDrawable());

      } else if(enabled){
        setBackgroundDrawable(background);  
        setColorFilter(null);
      }

      invalidateSelf();

      return super.onStateChange(states);
    }

}

}

这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">

<ImageButton
    android:id="@+id/title"
    android:layout_width="250dp"
    android:layout_height="58dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_margin ="25dp"
    android:background="@drawable/skintonetitle" />

<custombuttons.CustomImageButton
    android:id="@+id/skina1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/title"
    android:layout_below="@+id/title"
    android:layout_marginTop="35dp"
    android:background="@drawable/test_circle"
    android:clickable="true"
    android:focusable="true" />

</RelativeLayout>

我错过了什么?

最佳答案

它从 View 而不是按钮扩展,因此默认情况下它不可点击或不可聚焦。调整为

android:clickable="true"
android:focusable="true"

在您的 XML 中。

如果你想在 java 中这样做,你也可以在你的 View 类的构造函数中设置它们:

setFocusable(true);
setClickable(true);

关于android - 自定义 View 不响应触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10406354/

相关文章:

android - GridView 吃掉我的 onTouch 事件并开始 "scrolling"

android - 以编程方式禁用 android 非安全键盘锁

Android使用ScrollView控件拉动刷新

java - 根据 Android Intent ACTION_SEND 中的选择更改收件人电子邮件 ID

java - 使用另一个 fragment 中的按钮打开 fragment

android - 如何在多个 Activity 处于 Activity 状态时关闭应用程序

java - 在 Android Studio 中将图像覆盖在自定义 AlertDialog 之上

android - 复选框未显示在 android 布局上

android - 如何禁用 EditText 中的“仅剪切”选项?

安卓 : Widget - Null pointer Exception when using setOnclickPendingIntent on the remoteviews?