简体   繁体   English

单击后如何更改按钮的文本颜色,并在第二次单击时恢复为默认值?

[英]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 ”文本Likeblue ,并且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.

相关问题 在 javafx 中如何更改按钮的颜色,等待 1 秒然后将其改回默认值? - In javafx how do i change the color of a button, wait 1 second than change it back do default? 单击后如何保留jlabel的新背景颜色,并在第二次单击时恢复默认值? - How do I retain the new background colour of a jlabel after it has been clicked and to make it go back to default on a second click? 单击按钮后如何更改 textView 的文本? - How to change the text of a textView after a button is clicked? 单击后如何更改按钮,释放按钮后又如何恢复正常? - How to change the button upon click ,and revert back to normal upon release the button? 单击后更改按钮文本,然后使用JAVA ActionEvent再次单击后还原更改 - Change button text after click, then revert the changes after clicking again using JAVA ActionEvent 模拟点击后如何使按钮颜色恢复正常 - How to get button color back to normal after a simulated click 如何在按下按钮后将按钮恢复为默认颜色? - How to return a button to its default color after being pressed? 当按钮中的背景色调属性为默认时,如何更改 android 中单击按钮的背景颜色? - How to change the background color of a clicked button in android when background tint attribute is default in button? 如何检查秒后是否仍单击按钮 - How to check if Button is still clicked after second 试图在单击时来回更改 J 按钮的颜色 - Trying to change the color of a J Button back and forth when it is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM