简体   繁体   中英

How to set the text value of EditText at run time?

I have an edit text field and I want to set the value of this field(to an integer) dynamically at run time. As the EditText field accepts strings by default, how do I proceed with this?

Take a look at String.valueOf method :

int someInt = 5;
myEditText.setText(String.valueOf(someInt))

You can change the input type of the editText at the XML end to force the user to input Integer Values only.

As for setting the values programmatically at runtime, do this:

EditText editText = (EditText)findViewById(R.id.editText1);
int input = 4;
editText.setText(string.ValueOf(input));

set the property of edittext to accept integers only, if this is what you want by adding following property in your xml file that contains the edittext.

android:inputType="number"

For doing calculations, if the value entered is number you can use following function:-

EditText editText = (EditText)findViewById(R.id.editText);
int editTextValue = Integer.parseInt(editText.getText().toString());

now you can perform calculations on the same.

For setting an int to editText, you can use following:-

editText.setText(""+editTextValue);//here editTextValue is the number which you want to set in editText field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM