cristoph
cristoph

Reputation: 21

How can I read user input in Android?

I need to read a user input in android to process it later.

How can I do this?

In iPhone SDK I'd do something like this:

-(IBAction)command {
   system("echo %s", textfield.text");
}

Upvotes: 0

Views: 10937

Answers (2)

James
James

Reputation: 5642

Put this in you code:

final EditText edittext = (EditText) findViewById(R.id.edittext);

And this is the textbox, or whatever:

<EditText
    android:id="@+id/edittext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

And something like this to get the text of it:

edittext.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // If the event is a key-down event on the "enter" button
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER)) {
          // Perform action on key press
          Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
          return true;
        }
        return false;
    }
});

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006594

Put an EditText in your UI, and call getText().toString() on it when you need the value that the user typed in.

Upvotes: 1

Related Questions