android - how to convert int to string and place it in a EditText?

Deadly 提交于 2019-12-03 05:36:19

问题


I have this piece of code:

ed = (EditText) findViewById (R.id.box); 
int x = 10; 
ed.setText (x);

It turns out to be an error. I know I have to change it to string, but how do I do this?

I've tried x.toString(), but it can't be compiled.


回答1:


Use +, the string concatenation operator:

ed = (EditText) findViewById (R.id.box);
int x = 10;
ed.setText(""+x);

or use String.valueOf(int):

ed.setText(String.valueOf(x));

or use Integer.toString(int):

ed.setText(Integer.toString(x));



回答2:


try Integer.toString(integer value); method as

ed = (EditText)findViewById(R.id.box);
int x = 10;
ed.setText(Integer.toString(x));



回答3:


Try using String.format() :

ed = (EditText) findViewById (R.id.box); 
int x = 10; 
ed.setText(String.format("%s",x)); 



回答4:


Use this in your code:

String.valueOf(x);


来源:https://stackoverflow.com/questions/6659230/android-how-to-convert-int-to-string-and-place-it-in-a-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!