【问题标题】:Custom View not responding to touches自定义视图不响应触摸
【发布时间】:2012-05-02 00:49:27
【问题描述】:

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

这是我的代码:

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>

我错过了什么?

【问题讨论】:

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


    【解决方案1】:

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

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

    在您的 XML 中。

    如果你想在 java 中进行,你也可以在 View 类的构造函数中设置这些:

    setFocusable(true);
    setClickable(true);
    

    【讨论】:

    • 嗯,有道理。我下班后试试。我可以在我的自定义按钮类中默认使视图可点击和聚焦吗?
    • 是的-我的答案中的这两行是 XML 属性。您可以将它们放在
    • 对不起,我的意思是我可以在我的自定义按钮 src 中覆盖一个方法,使其默认可点击。所以我不需要在我的每个 xmls 中添加这些行。
    • 哦-对不起。在该视图的构造函数中,您可以调用 setFocusable(true) 和 setClickable(true)。
    • 刚刚测试过,仍然没有反应...嗯(对不起)同时添加到 xml 和 java.xml 中。编辑问题以显示更改。进行更改的代码是否有问题?
    【解决方案2】:

    在我的情况下,我使用了一个以约束布局为根的自定义视图。我没有在我的自定义视图的 setOnClickListener 上收到点击事件,结果我需要在我的根目录中设置 android:clickable="false" xml 用于自定义视图。显然,单击事件被调度到我的自定义视图 xml 的根而不是它自己的自定义视图(即自定义视图的 setOnClickListener)

    【讨论】:

    • 这一定是个错误。将您的根 clickable='false' 设置为什么...使其可点击是没有意义的。但是谢谢你让它为我工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    相关资源
    最近更新 更多