简体   繁体   English

无法比较字符串变成整数java

[英]Can't compare string turned into integer java

So, I need to change my textView color to red if it's more than 0 and to green if it's less than 0, but I can't write if statement properly, because textView is a string, but I've changed it to int.因此,我需要将 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));

    }

To work with the String inside the TextView (Which is not a String , it's a View ) you first need to access it with .getText().toString() .为了与工作String内部TextView (这是不是一个String ,它是一个View ),你首先需要访问它.getText().toString() So if you want to work with the value of the textView like an integer, do the following:因此,如果您想像整数一样处理 textView 的值,请执行以下操作:

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

If you want to set the value again to the view, make sure you're passing a String and not an integer.如果要再次为视图设置值,请确保传递的是String而不是整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM