Android控制软键盘的弹出和隐藏

简介: 弹出软键盘 前提:必须要有一个可以编辑的控件(EditText),并且当前已经获取焦点/** * 弹出软键盘 */public void openKeyboard(View view) { // 获取焦点 editText2.

弹出软键盘

前提:必须要有一个可以编辑的控件(EditText),并且当前已经获取焦点

/**
 * 弹出软键盘
 */
public void openKeyboard(View view) {
    // 获取焦点
    editText2.setFocusable(true);
    editText2.setFocusableInTouchMode(true);
    editText2.requestFocus();
    // 弹出软键盘
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(editText2, 0);
}

参数说明

/**
 * Synonym for {@link #showSoftInput(View, int, ResultReceiver)} without a result receiver: explicitly request that the current input method's soft input area be shown to the user, if needed.
 * 
 * @param view The currently focused view, which would like to receive soft keyboard input.
 * @param flags Provides additional operating flags.  Currently may be 0 or have the {@link #SHOW_IMPLICIT} bit set.
 */
public boolean showSoftInput(View view, int flags) {
    return showSoftInput(view, flags, null);
}
  • 效果图

P1

隐藏软键盘

/**
 * 关闭软键盘
 */
public void closeKeyboard(View view) {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

hideSoftInputFromWindow()方法的参数是一个IBinder窗口令牌,可以用View.getWindowToken()从附加到窗口的View对象上获得,大部分情况下,每个事件的回调方法都会有一个引用指向正在编辑的TextView,或者是发起事件的View(比如某个按钮),通过调用这些对象获得窗口令牌,再将其传递给InputMethodManager最方便的做法。

  • 效果图

P2

软键盘开关

开启软键盘的时候隐藏软键盘,隐藏软键盘的时候开启软键盘

/**
 * 软键盘开关
 */
public void toggleKeyboard(View view) {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInputFromWindow(view.getWindowToken(), 0, 0);
}

弹出键盘的时候是一个道理,想要编辑哪个EditText,要先让其获取焦点

  • 效果图

P3
P4

测试类

package com.kongqw.kqwkeysetdemo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;


public class MainActivity extends Activity {

    private EditText editText;
    private EditText editText2;

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

        editText = (EditText) findViewById(R.id.edittext);
        editText2 = (EditText) findViewById(R.id.edittext2);
    }

    /**
     * 弹出软键盘
     */
    public void openKeyboard(View view) {
        // 获取焦点
        editText2.setFocusable(true);
        editText2.setFocusableInTouchMode(true);
        editText2.requestFocus();
        // 弹出软键盘
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText2, 0);
    }

    /**
     * 关闭软键盘
     */
    public void closeKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    /**
     * 软键盘开关
     */
    public void toggleKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInputFromWindow(view.getWindowToken(), 0, 0);
    }
}

页面布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

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

    <EditText
        android:id="@+id/edittext2"
        android:layout_width="match_parent"
        android:layout_below="@+id/edittext"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edittext2"
        android:onClick="openKeyboard"
        android:text="弹出软键盘" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:onClick="closeKeyboard"
        android:text="关闭软键盘" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:onClick="toggleKeyboard"
        android:text="开关" />
</RelativeLayout>
相关文章
|
3月前
|
Android开发
解决在Android Compose中点击空白处收回软键盘
解决在Android Compose中点击空白处收回软键盘
|
4月前
|
XML 监控 Java
Android App开发之事件交互Event中检测软键盘和物理按键讲解及实战(附源码 演示简单易懂)
Android App开发之事件交互Event中检测软键盘和物理按键讲解及实战(附源码 演示简单易懂)
119 0
|
8月前
|
Android开发
Android 实现点击输入框以外的区域隐藏软键盘
Android 实现点击输入框以外的区域隐藏软键盘
95 0
|
8月前
|
Android开发
Android InputMethodManager进入页面后自动弹出软键盘
Android InputMethodManager进入页面后自动弹出软键盘
327 0
|
10月前
|
Android开发
android 自定义登陆对话框基类封装,且随着软键盘的弹起自动移动位置
android 自定义登陆对话框基类封装,且随着软键盘的弹起自动移动位置
|
10月前
|
Android开发
Android自定义实现漂亮的软键盘
Android自定义实现漂亮的软键盘
|
Android开发
Android点击空白区域,隐藏输入法软键盘
Android点击空白区域,隐藏输入法软键盘
595 0
|
Java Android开发
【安卓开发】Android中自定义软键盘的使用
【安卓开发】Android中自定义软键盘的使用
400 0
【安卓开发】Android中自定义软键盘的使用
|
Android开发 UED
完美解决android软键盘挡住输入框方法,还不顶标题栏
完美解决android软键盘挡住输入框方法,还不顶标题栏
1145 0
完美解决android软键盘挡住输入框方法,还不顶标题栏
|
Android开发
Android EditText之软键盘搜索
Android EditText之软键盘搜索
499 0
Android EditText之软键盘搜索