如何在 switch 语句 android studio 中设置颜色

问题描述 投票:0回答:1
 static BrickType fromValue(int value) {
        switch (value) {

            case 1:
                return T;
            case 2:
                return CHAIR;
            case 3:
                return STICK;
            case 4:
                return SQUARE;
            case 0:
            default:
                return L;

我尝试使用 display.setTextColor 但这是一个错误。请帮助我并指出我在哪里弄错了或者我应该在我的代码中添加什么顺便说一句我使用android studio创建我的代码

android flutter kotlin j
1个回答
0
投票

尝试下面的代码一次。我希望它能奏效。

您需要在 TextView 对象上使用 setTextColor() 方法。这是一个例子:

static BrickType fromValue(int value, TextView display) {
        switch (value) {

            case 1:
                display.setTextColor(Color.RED);
                return T;
            case 2:
                display.setTextColor(Color.BLUE);
                return CHAIR;
            case 3:
                display.setTextColor(Color.GREEN);
                return STICK;
            case 4:
                display.setTextColor(Color.YELLOW);
                return SQUARE;
            case 0:
            default:
                display.setTextColor(Color.BLACK);
                return L;
        }
    }

在此代码中,setTextColor() 方法在显示 TextView 对象上调用,传入所需的颜色值。请注意,您需要在文件顶部导入 Color 类才能使用它。

另外,请注意 fromValue() 方法现在采用 TextView 类型的第二个参数,它表示您要修改其文本颜色的 TextView 对象。当您在代码中调用此方法时,您需要传入一个有效的 TextView 对象。

© www.soinside.com 2019 - 2024. All rights reserved.