ImageButton
当用户按下时如何切换颜色ImageButton
?
我希望ImageButton
按下时在红色(电源关闭)和绿色(电源)之间切换。
代码:
ImageButton star, power;
Intent i;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
star = (ImageButton)findViewById(R.id.ibStar);
star.setOnClickListener(this);
power = (ImageButton)findViewById(R.id.ibPower);
power.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN ) {
power.setImageResource(R.drawable.power);
}
else if (event.getAction() == MotionEvent.ACTION_UP ) {
power.setImageResource(R.drawable.power_off);
}
return true;
}