【问题标题】:Custom image button with clicked functionality具有单击功能的自定义图像按钮
【发布时间】:2014-06-12 17:04:14
【问题描述】:

我知道这是一个愚蠢的问题,但谷歌搜索并没有给我一个好的答案。我的活动中有一个按钮,但是当我按下它时,我看不到它。虽然按钮工作得很好。我知道我必须在 xml 文件中做一些事情,但我不确定是什么。有没有一种方法可以自动让图片按钮稍微改变而无需我手动上传两张图片?

【问题讨论】:

标签: android android-layout android-imagebutton


【解决方案1】:
import android.content.Context;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import android.widget.Button;

/**
 * Applies a pressed state color filter or disabled state alpha for the button's background
 * drawable.
 *
 * @author shiki
 */
public class SAutoBgButton extends Button {

  public SAutoBgButton(Context context) {
    super(context);
  }

  public SAutoBgButton(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public SAutoBgButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  @Override
  public void setBackgroundDrawable(Drawable d) {
    // Replace the original background drawable (e.g. image) with a LayerDrawable that
    // contains the original drawable.
    SAutoBgButtonBackgroundDrawable layer = new SAutoBgButtonBackgroundDrawable(d);
    super.setBackgroundDrawable(layer);
  }

  /**
   * The stateful LayerDrawable used by this button.
   */
  protected class SAutoBgButtonBackgroundDrawable extends LayerDrawable {

    // The color filter to apply when the button is pressed
    protected ColorFilter _pressedFilter = new LightingColorFilter(Color.LTGRAY, 1);
    // Alpha value when the button is disabled
    protected int _disabledAlpha = 100;

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

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

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

      mutate();
      if (enabled && pressed) {
        setColorFilter(_pressedFilter);
      } else if (!enabled) {
        setColorFilter(null);
        setAlpha(_disabledAlpha);
      } else {
        setColorFilter(null);
      }

      invalidateSelf();

      return super.onStateChange(states);
    }

    @Override
    public boolean isStateful() {
      return true;
    }
  }

}

要使用它,只需像这样替换原来的按钮声明:

<Button
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:background="@drawable/button_blue_bg"
  android:text="Button with background image" />

这样做:

<com.buttondemo.SAutoBgButton
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:background="@drawable/button_blue_bg"
  android:text="Button with background image" />

这是使用此自定义按钮的示例输出:

希望成功

【讨论】:

    【解决方案2】:

    如果您愿意,您可以使用禁用按钮

       button.setEnabled(false);
    

    & 也可以使用

    再次启用
       button.setEnabled(true);
    

    编辑:

    你也可以尝试通过改变Button的背景颜色

     onClick()
     {
      button.setBackgroundColor(#CCCCCC);
     }
    

    【讨论】:

      猜你喜欢
      • 2019-01-01
      • 2015-03-15
      • 2011-12-12
      • 1970-01-01
      • 2021-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多