2016-04-24 53 views
-2

所以,我需要将我的textView颜色更改为红色,如果它大于0,并且如果它小于0则为绿色,但我无法正确写入if语句,因为textView是一个字符串,但我已经把它改为int。无法比较字符串转换为整数java

textView.setText(textView + ""); 


     if(textView > 0) { 
      textView.setTextColor(this.getResources().getColor(R.color.colorAccent)); 
     } 
     else if (textView < 0){ 
      textView.setTextColor(this.getResources().getColor(R.color.colorPrimary)); 
     } 
     else { 
      textView.setTextColor(this.getResources().getColor(R.color.colorPrimaryDark)); 

    } 
+0

'textView'似乎是一个GUI组件。你正在对'setText'调用你不能用'int'值做的事情。 –

+0

按如下方式重新排序:'textView.setText(“”+ textView);' – Vucko

+0

这种隐式转换方式将为您处理。 :d – Vucko

回答

0

为了与TextViewString工作(这是不是一个String,这是一个View)你首先需要与.getText().toString()访问它。所以,如果你想与像一个整数TextView的价值工作,做到以下几点:

String stringValue = textView.getText().toString(); 
int intValue = Integer.parseInt(stringValue); 
//Now do your logic. 

如果您想再次将该值设置为视图,请确保你传递一个String,而不是整数。