Android advanced way: setting and monitoring in the lower right corner of the soft keyboard

In the project, you may encounter the need to modify the button at the bottom right corner of the soft keyboard. Although you have written it several times, you still feel relieved to concentrate on taking notes here.

Effect :

Note (key points):

  • Xml medium

1. Confirm the function display keyboard in the lower right corner
android:imeOptions="actionSearch"

Such as:
actionGo : Go,
actionSearch: magnifier
actionSend : Send
actionNext : Next
actionDone: done

2. Determine the current EditText entry (I think it's literally a separate line)
android:singleLine="true"

3. Complete single EditText code

   <EditText
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@null"
        android:hint="search"
        android:imeOptions="actionSearch"
        android:singleLine="true"
        />
  • In code

1. Set action monitoring
For example: mSearch.setOnEditorActionListener(this);

2. Conduct action monitoring

@Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        boolean state = true;
        switch (actionId) {
            case EditorInfo.IME_ACTION_SEARCH:
                Toast.makeText(this, "Inside the soft keyboard - search", Toast.LENGTH_LONG).show();
                hintKeyboard();
                break;
            default:
                state = false;
                break;
        }
        return state;
    }

Hide keyboard method:

    /*Hide soft keyboard*/
    void hintKeyboard() {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager.isActive()) {
            inputMethodManager.hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), 0);
        }
    }

MainActivity :

package com.example.yongliu.keyboard;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, TextView.OnEditorActionListener {

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

        EditText mSearch = findViewById(R.id.search);
        EditText mNext = findViewById(R.id.next);
        EditText mDone = findViewById(R.id.done);
        EditText mGo = findViewById(R.id.go);
        EditText mSend = findViewById(R.id.send);

        mSearch.setOnClickListener(this);
        mNext.setOnClickListener(this);
        mDone.setOnClickListener(this);
        mGo.setOnClickListener(this);
        mSend.setOnClickListener(this);

        mSearch.setOnEditorActionListener(this);
        mNext.setOnEditorActionListener(this);
        mDone.setOnEditorActionListener(this);
        mGo.setOnEditorActionListener(this);
        mSend.setOnEditorActionListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search:
                Toast.makeText(this, "search - click", Toast.LENGTH_LONG).show();
                break;
            case R.id.next:
                Toast.makeText(this, "next step - click", Toast.LENGTH_LONG).show();
                break;
            case R.id.done:
                Toast.makeText(this, "complete - click", Toast.LENGTH_LONG).show();
                break;
            case R.id.go:
                Toast.makeText(this, "Go - click", Toast.LENGTH_LONG).show();
                break;
            case R.id.send:
                Toast.makeText(this, "Send out - click", Toast.LENGTH_LONG).show();
                break;
        }
    }

    @Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        boolean state = true;
        switch (actionId) {
            case EditorInfo.IME_ACTION_SEARCH:
                Toast.makeText(this, "Inside the soft keyboard - search", Toast.LENGTH_LONG).show();
                hintKeyboard();
                break;
            case EditorInfo.IME_ACTION_NEXT:
                Toast.makeText(this, "Inside the soft keyboard - next step", Toast.LENGTH_LONG).show();
                hintKeyboard();
                break;
            case EditorInfo.IME_ACTION_DONE:
                Toast.makeText(this, "Inside the soft keyboard - complete", Toast.LENGTH_LONG).show();
                hintKeyboard();
                break;
            case EditorInfo.IME_ACTION_GO:
                Toast.makeText(this, "Inside the soft keyboard - Go", Toast.LENGTH_LONG).show();
                hintKeyboard();
                break;
            case EditorInfo.IME_ACTION_SEND:
                Toast.makeText(this, "Inside the soft keyboard - Send out", Toast.LENGTH_LONG).show();
                hintKeyboard();
                break;
            default:
                state = false;
                break;
        }
        return state;
    }

    /*Hide soft keyboard*/
    void hintKeyboard() {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager.isActive()) {
            inputMethodManager.hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), 0);
        }
    }
}

MainActivity Xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.yongliu.keyboard.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:gravity="center"
        android:text="Soft keyboard monitoring"
        />

    <EditText
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@null"
        android:gravity="center|start"
        android:hint="search"
        android:imeOptions="actionSearch"
        android:paddingLeft="15dp"
        android:singleLine="true"
        android:textSize="13sp"
        />

    <EditText
        android:id="@+id/send"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@null"
        android:gravity="center|start"
        android:hint="Send out"
        android:imeOptions="actionSend"
        android:paddingLeft="15dp"
        android:singleLine="true"
        android:textSize="13sp"
        />

    <EditText
        android:id="@+id/go"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@null"
        android:gravity="center|start"
        android:hint="Go"
        android:imeOptions="actionGo"
        android:paddingLeft="15dp"
        android:singleLine="true"
        android:textSize="13sp"
        />

    <EditText
        android:id="@+id/next"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@null"
        android:gravity="center|start"
        android:hint="next step"
        android:imeOptions="actionNext"
        android:paddingLeft="15dp"
        android:singleLine="true"
        android:textSize="13sp"
        />

    <EditText
        android:id="@+id/done"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@null"
        android:gravity="center|start"
        android:hint="complete"
        android:imeOptions="actionDone"
        android:paddingLeft="15dp"
        android:singleLine="true"
        android:textSize="13sp"
        />
</LinearLayout>

Keywords: Android xml encoding

Added by texmansru47 on Sun, 03 May 2020 13:58:44 +0300