简体   繁体   English

Android更改按钮的文本颜色2秒钟

[英]Android change a button's text color for 2 seconds

I would like to change a button's text's color to green for 2 seconds, and then turn it back to original. 我想将按钮文本的颜色更改为绿色2秒钟,然后将其恢复为原始状态。 How can I do this? 我怎样才能做到这一点? My method is: 我的方法是:

private void changeColors() {
    Button option1Button = (Button) findViewById(R.id.option1Button);
    Button option2Button = (Button) findViewById(R.id.option2Button);
    Button option3Button = (Button) findViewById(R.id.option3Button);
    Button option4Button = (Button) findViewById(R.id.option4Button);

    Button[] buttons = {option1Button, option2Button, option3Button, option4Button};

    buttons[correctAnswerButtonID].setTextColor(Color.GREEN);
    SystemClock.sleep(2000);
//later changing it back to original

}

This doesn't work, the text changes to green AFTER the sleep method. 这不起作用,睡眠方法后文本变为绿色。 I want it to change to green, and then start the sleep process. 我希望它变为绿色,然后启动睡眠过程。 Thank you for your answers! 谢谢您的回答!

you can use animation 你可以使用动画

buttons[correctAnswerButtonID].setTextColor(Color.GREEN);
buttons[correctAnswerButtonID].animate().setDuration(2000).withEndAction(new Runnable() {
            @Override
            public void run() {
                // set color back to normal
                buttons[correctAnswerButtonID].setTextColor(Color.BLACK);
            }
        }).start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM