开发者

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:

-(IBActi开发者_如何学Goon)command {
   system("echo %s", textfield.text");
}


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


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;
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜