[英]Change String color depending on value in Java
我是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 ;
任何幫助或指導將不勝感激:)
字符串對象沒有顏色裝飾,所以這不是最好的方法
if (feedPerTooth >0.35){
String attrs = new TextAttributes(Color.BLUE).format(feedPerTooth);
您應該改用#setError
方法
diameter.setError( "feedPerTooth is to high!" )
我猜您想使用以下textView.setTextColor(Color.parseColor("#F5DC49"));
設置TextView
顏色: textView.setTextColor(Color.parseColor("#F5DC49"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.