2016-04-24 52 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,而不是整數。