![](/img/trans.png)
[英]In javafx how do i change the color of a button, wait 1 second than change it back do default?
[英]How to change text color of button after being click,and revert back to default at second clicked?
我有一个like
按钮,当用户第一次点击时,我想
1) like
按钮的文字颜色变为蓝色
2)将另一个LinearLayout
setVisibility
设置为visible
3) setText
到另一个Text
小部件到特定的字符串。
当用户第二次单击该按钮时,返回到以前的状态。因此,我使用ValueAnimator
来检测该按钮是否已被单击并更改文本的颜色。这很好,但是当我添加代码进行操作时在上面2和3中,ValueAnimator失效。
这是我的代码
holder.like.setOnClickListener(new View.OnClickListener() {
ValueAnimator buttonColorAnim = null; // to hold the button animator
@Override
public void onClick(View v) {
// first time this will be null
//here is not the 1st time
if(buttonColorAnim != null){
// reverse the color
buttonColorAnim.reverse();
// reset for next time click
buttonColorAnim = null;
//here set the linear layout gone.
holder.amountLayout.setVisibility(GONE);
// add your code here to remove from database
}
else {
//here is 1st time
final Button button = (Button) v;
// create a color value animator
buttonColorAnim = ValueAnimator.ofObject(new ArgbEvaluator(), Color.BLUE, Color.BLACK);
// add a update listener for the animator.
buttonColorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
// set the background color
button.setTextColor((Integer) animator.getAnimatedValue());
}
});
// start the animator..
buttonColorAnim.start();
//here I do the action 2 and 3
holder.amountLayout.setVisibility(VISIBLE);
holder.likeAmount.setText("You liked this!");
// add your code here to add to database
}
}
});
我得到的是,按键点击第一次时, amountLayout
显示, likeAmount
文本设置,但颜色like
按钮是保持不变的, like
按钮的文字颜色只在第二次改变点击。 我附上了我在下图得到的输出
单击“ Like
按钮之前的外观
这是第一次单击“ Like
按钮时的外观,假设“ Like
”文本Like
为blue
,并且You liked this
显示了“ Like
,但现在“ Like
文本颜色不变。
这是第二次单击“ Like
按钮时的外观,假设此处文本颜色应为黑色,而You liked this
颜色消失了,但现在是blue
。
有人请帮助我看一下我的逻辑,并指出正确的方向。感谢您的帮助。
更新 @rckrd的解决方案之后,正在将这行添加到我的代码中buttonColorAnim.setDuration(10000)
like
按钮like
文字颜色在第一次单击时不会立即变回blue
,但在第二次单击时仍保持blue
,经过一定时间后,只有变回black
。
有人可以提供一种解决方案吗?在此情况下,第一次单击按钮或第二次单击按钮,文本颜色都会立即改变吗?
动画设置为从蓝色变为黑色:
buttonColorAnim = ValueAnimator.ofObject(new ArgbEvaluator(), Color.BLUE, Color.BLACK);
按钮的文本颜色从一开始就为黑色,并且由于动画上未设置持续时间,因此将使用默认持续时间(300 ms)。 因此,您将看不到任何颜色变化。 反转动画时,文本颜色将从黑色变为蓝色。
如果增加了动画持续时间buttonColorAnim.setDuration(10000)
,您将能够看到文本从蓝色变为黑色。
您还可以使用ObjectAnimator
,这样就不必设置更新侦听器。 在下面的示例中,文本在1.5秒内从黑色更改为蓝色。
buttonColorAnim = ObjectAnimator.ofInt(button,"textColor", Color.BLACK,Color.BLUE);
buttonColorAnim.setEvaluator(new ArgbEvaluator());
buttonColorAnim.setDuration(1500);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.