簡體   English   中英

單擊后如何更改按鈕的文本顏色,並在第二次單擊時恢復為默認值?

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM