繁体   English   中英

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

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

我想将按钮文本的颜色更改为绿色2秒钟,然后将其恢复为原始状态。 我怎样才能做到这一点? 我的方法是:

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

}

这不起作用,睡眠方法后文本变为绿色。 我希望它变为绿色,然后启动睡眠过程。 谢谢您的回答!

你可以使用动画

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