Android EditText listens for the Enter key and handles two callbacks

  • 2021-10-15 11:29:32
  • OfStack

Preface

As we all know, on the interface with EditText controls, such as boarding, searching, etc., when the user finishes entering the content, clicking the Enter key to execute the logic will have a very good user experience, so today we will learn how to listen to EditText listening for the Enter key and deal with the two callback problems when listening.

Code

First, we add an EditText to listen to in the xml layout


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <EditText
  android:id="@+id/et_text"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" />
</LinearLayout>

If you want to listen to what keys are pressed by the soft keyboard, you need to use the setOnKeyListener method. In Activity, we write the listening code


@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main2);

  EditText et = findViewById(R.id.et_text);
  et.setOnKeyListener(new View.OnKeyListener() {
   @Override
   public boolean onKey(View v, int keyCode, KeyEvent event) {
    return false;
   }
  });
 }

Next listen for the Enter key in the onKey method


@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main2);

  EditText et = findViewById(R.id.et_text);
  et.setOnKeyListener(new View.OnKeyListener() {
   @Override
   public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_ENTER){
     Log.e("MainActivity", "onKey:  Press the Enter key ");
     return true;
    }
    return false;
   }
  });
 }

When we run and click Enter in EditText, we will see the following log

E/MainActivity: onKey: Press Enter
E/MainActivity: onKey: Press Enter

Why do we see two log entries?

Answer and solve the mediation twice

As we all know, when we press a key, the keyboard has two operations: press and lift, so the two callbacks here are one callback when press Enter and one callback when lift Enter.

To solve this problem is very simple, we only need to add one more condition to judge whether to lift or press


@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main2);

  EditText et = findViewById(R.id.et_text);
  et.setOnKeyListener(new View.OnKeyListener() {
   @Override
   public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
     Log.e("MainActivity", "onKey:  Press the Enter key ");
     return true;
    }
    return false;
   }
  });
 }

Let's run it again. After clicking Enter, the log will be output

E/MainActivity: onKey: Press Enter

You can also modify the carriage return style of editText, that is, set the ImeOptions attribute of EditText to a different value, and different words or patterns will be displayed on the Enter key

actionNone: Enter, press the rear cursor to the next row
actionGo: Go,
actionSearch: 1 magnifying glass
actionSend: Send
actionNext: Next
actionDone: Done, hidden soft keyboard, even if not the last text input box


Related articles: