Android切换按钮 - 绿色变为红色
答
@Override
public boolean onTouch(final View view, MotionEvent event) {
final int action = event.getAction();
if(view.getId()==R.id.yourButton){
if(action == MotionEvent.ACTION_DOWN)
yourButton.setBackgroundResource(R.drawable.ic_button_green);
if(action == MotionEvent.ACTION_UP)
yourButton.setBackgroundResource(R.drawable.ic_button_red);
}
}
阅读这篇文章可能对您有帮助。 http://www.mkyong.com/android/android-imagebutton-selector-example/ – Akshay 2012-08-16 15:38:24
你应该使用'选择器'。按照@Akshay给出的链接,有一个简单的教程,演示选择器的创建和使用。 – 2012-08-16 15:42:21
@Akshay的解决方案是最好的。添加触摸监听器将成为开销。 – 2012-08-16 16:20:30