根据Java中的值更改字符串颜色

时间:2016-09-11 12:26:30

标签: java android

我是Java编程的新手,并尝试在Android中创建一个应用程序,其值根据计算值改变颜色。

public void calculate(View view) {
    EditText spindleSpeed = (EditText) findViewById(R.id.spindle_speed);
    String speedString=spindleSpeed.getText().toString();
    int speed=Integer.parseInt(speedString);                 
    EditText diameter = (EditText) findViewById(R.id.tool_diameter);
    String diaString=diameter.getText().toString();   
    int dia=Integer.parseInt(diaString);            
    EditText feedRate = (EditText) findViewById(R.id.feed_rate);
    String feedString=feedRate.getText().toString();   
    int feed=Integer.parseInt(feedString);             
    EditText numberOfTeeth = (EditText) findViewById(R.id.number_of_teeth);
    String teethString=numberOfTeeth.getText().toString();   
    int teeth=Integer.parseInt(teethString);                
    double feedPerTooth = ((feed*1.0)/(speed*teeth));
    double cuttingSpeed = (dia*3.14*speed)/(1000);
    String   FPT = new DecimalFormat("0.00").format(feedPerTooth);
    String surfaceSpeed = new DecimalFormat("0.00").format(cuttingSpeed);

下面这部分我遇到了麻烦,如何让我的文字根据价值改变颜色?

    if (feedPerTooth >0.35){
        String attrs = new TextAttributes(Color.BLUE).format(feedPerTooth;
       FPT.getColor("#FF0000").format(feedPerTooth);
    } else if (feedPerTooth<0.07){
        FPT = "<font color=#FF0000>";
    } else ;

任何帮助或指导将不胜感激:)

2 个答案:

答案 0 :(得分:0)

字符串对象没有颜色属性,所以这不是最佳方式

 if (feedPerTooth >0.35){
        String attrs = new TextAttributes(Color.BLUE).format(feedPerTooth);

您应该使用方法#setError

 diameter.setError( "feedPerTooth is to high!" ) 

答案 1 :(得分:0)

我想你想设置package net.lenthe; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; @Component public class MessageListenerBean { private Logger logger = LoggerFactory.getLogger(this.getClass()); @JmsListener(destination = "myMessageTopic", containerFactory = "topicJmsListenerContainerFactory") public void processTopicMessage(String content) { logger.info("Received topic message. Content is " + content); } @JmsListener(destination = "myMessageQueue") public void processQueueMessage(String content) { logger.info("Received queue message. Content is " + content); } } 的颜色使用它:TextView