我想在不使用任何图像的情况下按下并释放按钮时突出显示一个按钮,因此我正在使用 SO 上的帖子中的以下代码,但它对我不起作用:
public class MainActivity extends Activity {
ImageButton myButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (ImageButton) findViewById(R.id.button);
myButton.setOnTouchListener(new ButtonHighlighterOnTouchListener(myButton));
}
public class ButtonHighlighterOnTouchListener implements OnTouchListener {
final ImageButton imageButton;
public ButtonHighlighterOnTouchListener(final ImageButton imageButton) {
super();
this.imageButton = imageButton;
}
@Override
public boolean onTouch(final View view, final MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
//grey color filter, you can change the color as you like
imageButton.setColorFilter(Color.parseColor("#5F2444"));
System.out.println("called down");
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageButton.setColorFilter(Color.parseColor("#245F30"));
System.out.println("called up");
}
return false;
}
}
这里有什么问题?
原帖在这里:如何使按钮突出显示?