ilana
ilana

Reputation: 21

Reading from and writing to EditText

What is the best way to read the text from an EditText into code, and to write some text from code to the EditText?


Sorry I have ment not the TextView but the EditText

Hi all

I am a new to android I wish to write automatically from code to EditText and read in code from EditText

What is the best way to do it.

Upvotes: 2

Views: 11129

Answers (1)

exhuma
exhuma

Reputation: 21757

Java classes usually expose readable attributes with a get* method, and writable attributes with a set* method. In the case of a EditText these are:

getText

and

setText

see here and here (they are inherited from TextView)

Note: Scroll around a bit. You will see that they are defined multiple time. With different parameters. Pick the one you need.

A simple example. Let's assume you have a TextView with the id myTextField:

EditText myText = (EditText) this.findViewById(R.id.myTextField);

// Setting the text:
myText.setText( "Hello World!" );

// "Reading" the text (printing it to stdout):
System.out.println( myText.getText() );

Upvotes: 2

Related Questions