在不使用两个drawable的情况下按下按钮时突出显示一个按钮?
之前我使用两个可绘制的按钮状态时按下,另一个作为默认值。所以我正在为了简单的效果制作一个洞新的图像!所以,现在我问...在不使用两个drawable的情况下按下按钮时突出显示一个按钮?
是否可以应用(颜色过滤器或任何效果)的按钮可按钮时按下按钮?
我发现做到这一点的最好办法!
Drawable drawableNotPressed = button.getBackground();
Drawable drawable = drawableNotPressed.getCurrent();
//use a filter
drawable.setColorFilter(0xF00, Mode.MULTIPLY);
StateListDrawable listDrawable = new StateListDrawable();
listDrawable.addState(new int[] { android.R.attr.state_pressed }, drawable);
listDrawable.addState(new int[] { android.R.attr.defaultValue }, drawableNotPressed);
button.setBackgroundDrawable(listDrawable);
et Voila!
你可以试着改变按钮的风格在西顿·监听方法之一
如果您的按钮是标准的灰色背景,你可以在onClick
方法应用过滤器的按钮来改变颜色.. 。
import android.graphics.PorterDuff;
button.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY); //Green
以后要清除过滤器:
button.getBackground().clearColorFilter();
过滤器的想法很好,但它在点击上的实用性并不好,因为按钮可以被按下但没有被点击......我可以听新闻活动吗? – 2012-08-01 23:17:54
我相信你可以,我以前没有这样做过,所以我没有第一手的经验来分享它。 – Barak 2012-08-01 23:45:54
或者你也可以,而不是使用setOnClickListner使用setOnTouchListener获得预期的效果只用一个形象
((Button)findViewById(R.id.testBth)).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
Button view = (Button) v;
view.getBackground().setColorFilter(0x77000000, PorterDuff.Mode.SRC_ATOP);
v.invalidate();
break;
}
case MotionEvent.ACTION_UP:
// Your action here on button click
case MotionEvent.ACTION_CANCEL: {
Button view = (Button) v;
view.getBackground().clearColorFilter();
view.invalidate();
break;
}
}
return true;
}
});
setOnKeyListener用于为例?并监听KeyEvent.ACTION_DOWN和KeyEvent.ACTION_UP?你可以说得更详细点吗 ? – 2012-08-01 14:36:49
@ M'hamed也许是setOnTouchListener,但我不确定。 – 2012-08-01 16:09:08