【问题标题】:change text color based on value of text根据文本的值更改文本颜色
【发布时间】:2019-03-05 06:30:16
【问题描述】:

我试图弄清楚如何根据文本的值更改 TextView 的颜色。 TextView 已从另一个活动发送,我的那部分工作正常。我想要的是一种根据 TextView 中的内容更改文本颜色的方法。因此,如果之前的 Activity 将“11 Mbps”之类的值作为 TextView 发送,那么我希望该文本颜色为黄色、“38 Mbps”绿色和 1 Mbps 红色。如果有帮助的话,我正在使用eclipse。

这就是我将 TextView 发送到另一个活动的方式。 “showmsg”只是发送到另一个页面的用户名。

buttonBack.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v){
            final TextView username =(TextView)findViewById(R.id.showmsg);
            String uname = username.getText().toString();

            final TextView wifistrength =(TextView)findViewById(R.id.Speed);
            String data = wifistrength.getText().toString();



                startActivity(new Intent(CheckWiFiActivity.this,DashboardActivity.class).putExtra("wifi",(CharSequence)data).putExtra("usr",(CharSequence)uname));


        }
    });

这就是我在其他活动中收到它的方式

Intent i = getIntent();
               if (i.getCharSequenceExtra("wifi") != null) {
                final TextView setmsg2 = (TextView)findViewById(R.id.Speed);
                setmsg2.setText(in.getCharSequenceExtra("wifi"));               
               }

这一切都很好,但我不知道如何根据文本的值更改 TextView 的颜色。任何帮助将不胜感激。

【问题讨论】:

    标签: android textview


    【解决方案1】:

    您显然想根据您从上一个活动中收到的String 中的数字设置颜色。所以你需要把String解析出来,保存到int,然后根据数字是什么,设置你的TextView的颜色。

    String s = in.getCharSequenceExtra("wifi");
    // the next line parses the number out of the string
    int speed = Integer.parseInt(s.replaceAll("[\\D]", ""));
    setmsg2.setText(s);
    // set the thresholds to your liking
    if (speed <= 1) {
        setmsg2.setTextColor(Color.RED);
    } else if (speed <= 11) {
        setmsg2.setTextColor(Color.YELLOW);
    else {
        setmsg2.setTextColor(Color.GREEN);
    }
    

    请注意这是一个未经测试的代码,它可能包含一些错误。

    解析方式来自here

    【讨论】:

    • +1,我也没有测试过,但看起来它可以工作。我开始发帖,当我看到你的时几乎没有发帖,但我喜欢尽可能使用switch,并且只用一行来调用函数(这里是setText()。很好的答案
    • 这正是我想要的。我只是复制并粘贴了,但我会分解这段代码的每一部分,以了解更多关于编程的知识并最终对其进行扩展。我只做了几个月,但感谢像你这样乐于助人的程序员,我取得了很大的进步。非常感谢您对此提供的帮助。
    【解决方案2】:

    首先,从String 中取出所有非数字字符并将其转换为integer。然后在新值上使用switch 并相应地设置颜色

    String color = "blue";   // this could be null or any other value but I don't like initializing to null if I don't have to
    int speed = i.getCharSequenceExtra("wifi").replaceAll("[^0-9]", "");    // remove all non-digits here
    switch (speed)
    {
        case (11):
            color = "yellow";
            break;
        case (38):
            color = "green";
            break;
        case(1):
            color = "red";
            break;
    }
    setmsg2.setTextColor(Color.parseColor(color);
    

    Here is a little site with some handy information

    Color Docs

    【讨论】:

    • 这个答案也很有帮助,但我不能投票,因为我没有足够的代表。也感谢您的链接。
    • @SmulianJulian 没问题,很高兴我能帮上忙
    • 好吧,这里switch 语句的问题是它只在速度正好为1、11 或38 时处理。如果是别的东西,那么文本将保持蓝色。但也许在他的情况下,价值不可能是别的东西,谁知道呢。只是说它和我的代码不一样。
    猜你喜欢
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 2019-07-08
    • 2016-06-02
    • 2021-11-17
    • 1970-01-01
    • 2019-11-25
    • 2021-11-14
    相关资源
    最近更新 更多