[英]ImageButton - toggle color when pressed
How can i toggle the color of an ImageButton
when user presses the ImageButton
? 我怎样才能触发一个颜色
ImageButton
当用户按下ImageButton
?
I want the ImageButton
to toggle between red (power off) and green (power) when pressed. 我希望
ImageButton
在按下时在红色(关闭电源)和绿色(关闭电源)之间切换。
Code: 码:
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;
}
I think this is what you need. 我认为这就是您所需要的。
ImageButton star, power;
Intent i;
boolean isOn=false;
@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 ) {
isOn=!isOn; // change its state to the oposite one
if(isOn)
power.setImageResource(R.drawable.power);
else
power.setImageResource(R.drawable.power_off);
}
return true;
}
Try using the OnClickListener
instead of the OnTouch
. 尝试使用
OnClickListener
而不是OnTouch
。 There are a few ways you can handle this situation. 您可以通过几种方法来处理这种情况。
One way is to have a boolean
that toggles between true
and false
signifying if the power is on or off. 一种方法是让一个
boolean
在true
和false
之间切换,以表示电源是打开还是关闭。 And then check that value when the ImageButton
is pressed and change the color of the ImageButton
accordingly. 然后在按下
ImageButton
时检查该值,并相应地更改ImageButton
的颜色。
/* The boolean below is
* declared in the
* class and not in a method.
* Whatever default value you need, i assumed false. */
private boolean isPowerOn = false;
power = (ImageButton)findViewById(R.id.ibPower);
power.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (isPowerOn) {
/* set color to red */
isPowerOn = false;
} else {
/* set power to green */
isPowerOn = true;
}
}
});
OR 要么
You can check the color of the ImageButton
when it is pressed and then change the color accordingly. 您可以在按下
ImageButton
时检查其颜色,然后相应地更改其颜色。 This way seems a little funky. 这样看来有点时髦。 Use the
boolean
method. 使用
boolean
方法。
If you want to have the button press event show a different color (ie during the press event), use a selector, like shown at Button Background Selector . 如果要让按钮按下事件显示不同的颜色(即在按下事件期间),请使用选择器,如按钮背景选择器所示。
If you want to have the button color toggle between red and green after an onClick event, then use an onClickListener() (pseudo-code): 如果要在onClick事件之后使按钮颜色在红色和绿色之间切换,请使用onClickListener()(伪代码):
onClick(ImageButton ib)
{
if (button is green)
ib.setImageResource(red)
else
ib.setImageResource(green)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.