P
P
Pavel Ivanov2013-12-05 07:39:24
Android
Pavel Ivanov, 2013-12-05 07:39:24

Why is input button click not handled in EditText?

When typing text in an EditText, after pressing the enter button, it wraps to another line, although this is not at all necessary. How can text input be handled? When orienting Landscape, the Next button appears and the processing of its click is triggered.
The code itself:

tv = (TextView) findViewById(R.id.textView);
editText = (EditText) findViewById(R.id.editText);
editText.setOnKeyListener(new View.OnKeyListener() {
  @Override
  public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
    if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
      tv.setText(editText.getText().toString());
      return true;
    }   
    return false;
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Ivanov, 2013-12-05
@eastywest

I came to this solution:
EditText has android:imeOptions="actionNext"
And then the following handler code:

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  @Override
  public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
    tv.setText(editText.getText().toString());
    return true;
  }
});

N
Nikiti4, 2013-12-05
@Nikiti4

&& is a shorthand for AND. To execute the code in if, you need to perform two clicks at the same time. Change to shorthand OR.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question