[英]How to change text color of button after being click,and revert back to default at second clicked?
I have a like
button,when user clicked at the 1st time,I want 我有一个
like
按钮,当用户第一次点击时,我想
1)Text color of like
button change to blue color 1)
like
按钮的文字颜色变为蓝色
2) setVisibility
of another LinearLayout
to visible
2)将另一个
LinearLayout
setVisibility
设置为visible
3) setText
to another Text
widget to certain string. 3)
setText
到另一个Text
小部件到特定的字符串。
When user click the button for the second time,reverse to the previous state.Therefore I use ValueAnimator
to detect whether the button have been clicked before and change the color of text.This works well,but when I add the code to do the action of 2 and 3 above,the ValueAnimator lose effect. 当用户第二次单击该按钮时,返回到以前的状态。因此,我使用
ValueAnimator
来检测该按钮是否已被单击并更改文本的颜色。这很好,但是当我添加代码进行操作时在上面2和3中,ValueAnimator失效。
Here is my code 这是我的代码
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
}
}
});
What I get is,button clicked at the 1st time, amountLayout
showing, likeAmount
text is set,but the color of like
button is remain the same, like
button text color only changed at the second time clicked. 我得到的是,按键点击第一次时,
amountLayout
显示, likeAmount
文本设置,但颜色like
按钮是保持不变的, like
按钮的文字颜色只在第二次改变点击。 I attached the output I get at the image below 我附上了我在下图得到的输出
This is outlook before clicking the Like
button 单击“
Like
按钮之前的外观
This is the outlook when Like
button being clicked in the First time,suppose the text Like
should turn to blue
color and the You liked this
shown up,but now the Like
text color not change. 这是第一次单击“
Like
按钮时的外观,假设“ Like
”文本Like
为blue
,并且You liked this
显示了“ Like
,但现在“ Like
文本颜色不变。
This is the outlook when Like
button being clicked in the Second time,suppose here text color should be black and You liked this
is gone,but now it is blue
. 这是第二次单击“
Like
按钮时的外观,假设此处文本颜色应为黑色,而You liked this
颜色消失了,但现在是blue
。
Somebody please help me take a look on my logic and point me to the right direction.Any help is appreciated. 有人请帮助我看一下我的逻辑,并指出正确的方向。感谢您的帮助。
UPDATE After following solution of @rckrd,which is adding this line in my code buttonColorAnim.setDuration(10000)
更新 @rckrd的解决方案之后,正在将这行添加到我的代码中
buttonColorAnim.setDuration(10000)
the like
button text color doesnt turn back to change immediately to blue
in 1st click,but still remain blue
in the second click,after a certain time,then only it change back to black
. like
按钮like
文字颜色在第一次单击时不会立即变回blue
,但在第二次单击时仍保持blue
,经过一定时间后,只有变回black
。
Can someone provide a solution,where the text color will change immediately either the 1st click the button or 2nd click of button? 有人可以提供一种解决方案吗?在此情况下,第一次单击按钮或第二次单击按钮,文本颜色都会立即改变吗?
The animation is set up to go from blue to black: 动画设置为从蓝色变为黑色:
buttonColorAnim = ValueAnimator.ofObject(new ArgbEvaluator(), Color.BLUE, Color.BLACK);
The text color of the button is black from start and since no duration is set on the animation the default duration (300 ms) will be used. 按钮的文本颜色从一开始就为黑色,并且由于动画上未设置持续时间,因此将使用默认持续时间(300 ms)。 Thus, you will not see the any change of color.
因此,您将看不到任何颜色变化。 When the animation is reversed the text color will change from black to blue.
反转动画时,文本颜色将从黑色变为蓝色。
If the animation duration is increased, buttonColorAnim.setDuration(10000)
, you will be able to see that your text change from blue to black. 如果增加了动画持续时间
buttonColorAnim.setDuration(10000)
,您将能够看到文本从蓝色变为黑色。
You can also use the ObjectAnimator
, this way you don't have to set a update listener. 您还可以使用
ObjectAnimator
,这样就不必设置更新侦听器。 In the below example the text is changed from black to blue during 1.5 secods. 在下面的示例中,文本在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.