控件設置OnTouchListener,代碼如下(控件在xml中需要設置background):
btn.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Drawable drawable = v.getBackground();
if (drawable == null)
return false;
if (Integer.parseInt(Build.VERSION.SDK) > Build.VERSION_CODES.DONUT)// 1.6版本以上使用
{
drawable.mutate();
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
drawable.setColorFilter(Color.argb(100, 0, 0, 0), Mode.DST_IN); //此處值可自行調整
v.setBackgroundDrawable(drawable);
break;
case MotionEvent.ACTION_UP:
drawable.clearColorFilter();
v.setBackgroundDrawable(drawable);
break;
case MotionEvent.ACTION_CANCEL:
drawable.clearColorFilter();
v.setBackgroundDrawable(drawable);
break;
default:
break;
}
return false;
}
});
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11