0

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");
}
jscs
  • 63,694
  • 13
  • 151
  • 195
cristoph
  • 21
  • 1
  • 1
  • 4

2 Answers2

1

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

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;
    }
});
James
  • 5,622
  • 9
  • 34
  • 42