[英]Can't compare string turned into integer java
因此,我需要將 textView 顏色更改為大於 0 的紅色和小於 0 的綠色,但我無法正確編寫 if 語句,因為 textView 是一個字符串,但我已將其更改為 int。
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));
}
為了與工作String
內部TextView
(這是不是一個String
,它是一個View
),你首先需要訪問它.getText().toString()
。 因此,如果您想像整數一樣處理 textView 的值,請執行以下操作:
String stringValue = textView.getText().toString();
int intValue = Integer.parseInt(stringValue);
//Now do your logic.
如果要再次為視圖設置值,請確保傳遞的是String
而不是整數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.