android - how to convert int to string and place it in a EditText?
162,461
Solution 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));
Solution 2
try Integer.toString(integer value);
method as
ed = (EditText)findViewById(R.id.box);
int x = 10;
ed.setText(Integer.toString(x));
Solution 3
Try using String.format()
:
ed = (EditText) findViewById (R.id.box);
int x = 10;
ed.setText(String.format("%s",x));
Solution 4
ed.setText (String.ValueOf(x));
Author by
Jason
Updated on March 04, 2021Comments
-
Jason about 3 years
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.