如何将int转换为String?

28 java string

我有一个int变量,当我将此变量设置为Android TextView的文本时,它会抛出一个错误,可能是因为它是一个Int.我已经检查但是找不到int的toString函数.那我该怎么办呢?

int sdRate=5;
//text_Rate is a TextView
text_Rate.setText(sdRate); //gives error
Run Code Online (Sandbox Code Playgroud)

Kon*_*rov 97

使用String.valueOf():

int sdRate=5;
//text_Rate is a TextView
text_Rate.setText(String.valueOf(sdRate)); //no more errors
Run Code Online (Sandbox Code Playgroud)

  • 我想知道`String.valueOf`和`Integer.toString`有什么区别? (4认同)

Sco*_* M. 13

使用Integer类的静态toString()方法.

int sdRate=5;
text_Rate.setText(Integer.toString(sdRate));
Run Code Online (Sandbox Code Playgroud)