android - Jak przekonwertować int na string i umieścić go w EditText?

Mam ten fragment kodu:

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

Okazuje się, że to błąd. Wiem, że muszę zmienić to na string, ale jak to zrobić?

Próbowałem x.toString(), ale nie można go skompilować.

Author: Adrian Cid Almaguer, 2011-07-12

4 answers

Use +, Thestring concatenation operator:

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

Lub użyć String.valueOf(int):

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

Lub użyć Integer.toString(int):

ed.setText(Integer.toString(x));
 86
Author: Matt Ball,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-06 10:50:47

Wypróbuj metodę Integer.toString(integer value); jako

ed = (EditText)findViewById(R.id.box);
int x = 10;
ed.setText(Integer.toString(x));
 9
Author: Rob,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-24 05:25:30

Spróbuj użyć String.format():

ed = (EditText) findViewById (R.id.box); 
int x = 10; 
ed.setText(String.format("%s",x)); 
 2
Author: Ti Kanon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-21 16:42:15

Użyj tego w swoim kodzie:

String.valueOf(x);
 0
Author: Aparajita Sinha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-21 10:48:59