Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 667
Jason Avatar asked Jul 12 '11 03:07

Jason


People also ask

How do you convert int to string manually?

Conversion of an integer into a string by using to_string() method. The to_string() method accepts a single integer and converts the integer value or other data type value into a string.

Can ints be added to strings?

The easiest way to convert int to String is very simple. Just add to int or Integer an empty string "" and you'll get your int as a String. It happens because adding int and String gives you a new String. That means if you have int x = 5 , just define x + "" and you'll get your new String.

How can I change text to int in Android Studio?

The Best Answer is you have to used. String value= et. getText(). toString(); int finalValue=Integer.


1 Answers

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)); 
like image 198
Matt Ball Avatar answered Oct 11 '22 10:10

Matt Ball