【发布时间】:2013-04-20 22:39:02
【问题描述】:
我想在按下并释放时突出显示一个按钮而不使用任何图像,所以我使用 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;
}
}
这里有什么问题?
【问题讨论】:
-
你需要点击/悬停效果吗?
-
@Abhi 你好哥们我需要点击效果哥们
-
你为什么不简单地使用选择器?
-
我需要以编程方式进行
-
@Abhi 我自己解决了我需要你的更多帮助