开发者

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

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.


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));


try Integer.toString(integer value); method as

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


Try using String.format() :

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


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


Use this in your code:

String.valueOf(x);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜