android - how to convert int to string and place i

2019-03-09 17:21发布

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.

4条回答
劫难
2楼-- · 2019-03-09 17:51

Try using String.format() :

ed = (EditText) findViewById (R.id.box); 
int x = 10; 
ed.setText(String.format("%s",x)); 
查看更多
一夜七次
3楼-- · 2019-03-09 17:52

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));
查看更多
Animai°情兽
4楼-- · 2019-03-09 17:56

try Integer.toString(integer value); method as

ed = (EditText)findViewById(R.id.box);
int x = 10;
ed.setText(Integer.toString(x));
查看更多
一纸荒年 Trace。
5楼-- · 2019-03-09 18:12

Use this in your code:

String.valueOf(x);
查看更多
登录 后发表回答