How can I read user input in Android?

前端 未结 2 455
臣服心动
臣服心动 2021-01-25 21:29

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 {
           


        
相关标签:
2条回答
  • 2021-01-25 22:03

    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 讨论(0)
  • 2021-01-25 22:30

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

    0 讨论(0)
提交回复
热议问题