Android EditText 软键盘的显示和隐藏的那些事

activity有这么一个属性 android:windowSoftInputMode,我们先看看各个值的含义

  • 【A】stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置
  • 【B】stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activity里的状态,无论是隐藏还是显示
  • 【C】stateHidden:用户选择activity时,软键盘总是被隐藏
  • 【D】stateAlwaysHidden:当该Activity主窗口获取焦点时,软键盘也总是被隐藏的
  • 【E】stateVisible:软键盘通常是可见的
  • 【F】stateAlwaysVisible:用户选择activity时,软键盘总是显示的状态
  • 【G】adjustUnspecified:默认设置,通常由系统自行决定是隐藏还是显示
  • 【H】adjustResize:该Activity总是调整屏幕的大小以便留出软键盘的空间
  • 【I】adjustPan:当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分

那么不让软键盘挡住EditText的方法 
在manifest中配置 

<activity android:windowSoftInputMode="stateVisible|adjustResize". . . /> 

 android:windowSoftInputMode

activity主窗口与软键盘的交互模式,可以用来避免输入法面板遮挡问题,Android1.5后的一个新特性。

这个属性能影响两件事情:

【一】当有焦点产生时,软键盘是隐藏还是显示

【二】是否减少活动主窗口大小以便腾出空间放软键盘

它的设置必须是下面列表中的一个值,或一个”state…”值加一个”adjust…”值的组合。在任一组设置多个值——多个”state…”values,例如&mdash有未定义的结果。各个值之间用|分开。例如:<activity android:windowSoftInputMode="stateVisible|adjustResize". . . >

在这设置的值(除"stateUnspecified"和"adjustUnspecified"以外)将覆盖在主题中设置的值。

使用程序代码关闭输入法:

1,设置setSoftInputMode

private void setSoftInputMode(boolean isVisible){
    if (HISTORYKEY.equals(Constants.SP_KEY_SEARCH_CONTENT_ARTICAL)){
        if (isVisible){
            this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        }else {
            this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        }

    }
}

2,EditText 隐藏显示软键盘

//隐藏软键盘
private void hideSoftKeyboard(Context context){
    if (editText == null || context == null)return;
    InputMethodManager imm = (InputMethodManager) context. getSystemService(context.INPUT_METHOD_SERVICE);
    if (imm == null)return;
    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); //强制隐藏键盘
}
//显示软键盘
private void showSoftKeyboard(Context context){
    if (editText == null || context == null)return;
    InputMethodManager imm = (InputMethodManager)context. getSystemService(context.INPUT_METHOD_SERVICE);
    if (imm == null)return;
    imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}

在EditText操作软键盘的时候,经常需要取消EditText的焦点

public void clearFocusable(Context context){
    if (editText == null)return;
    if (framelayout == null)return;
    framelayout.requestFocus();
    framelayout.setFocusable(true);//framelayout 为editText的父控件
    framelayout.setFocusableInTouchMode(true); //先用父控件获取焦点,然后再取消掉editText的焦点
    editText.clearFocus();
    editText.setSelected(false);
    //隐藏软键盘
    hideSoftKeyboard(context);
}

EditText的属性设置

  • 常用的一些属性设置
属性值 含义
android:hint 设置提示的信息,常见如:“请输入要搜索的内容”等
android:textColorHint 设置提示信息的文字颜色,就是上面的hint的颜色
android:maxLines 顾名思义就是设置最多显示的行数
android:maxLength 控制最大的输入长度
android:focusableInTouchMode 设置点击是否能获取焦点,如果设置为false,则点击EditText也获取不到焦点
android:focusable 也是设置是否能获取焦点,focusableInTouchMode=”true”,一定会使focusable=“true”;focusable=“false”,一定会使focusableInTouchMode=“false”。所以要使EditText获取不到焦点,设置任意一个为false即可
android:cursorVisible 控制光标显示,false则光标不显示,默认为true
android:textCursorDrawable 控制光标的颜色,如果设置为“@null”则颜色和text color一样
android:imeOptions 可以根据需要设置不同的值来达到不同的效果,如果需要自定义回车的按钮功能必须同时设置android:singleLine=”true”或是设置android:inputType
android:inputType 参考:https://developer.android.com/training/keyboard-input/style.html#Action

android:imeOptions的属性含义

属性值 效果
“flagNoExtractUi” 使软键盘不全屏显示,只占用一部分屏幕
“actionNone” 输入法回车键不带任何提示
“actionGo” 输入法回车键为“开始”
“actionSearch” 输入法回车键为“搜索”
“actionSend” 输入法回车键为“发送”
“actionNext” 输入法回车键为“下一个”
“actionDone” 输入法回车键为“完成”

需要注意的是要想使用回车的按钮功能,必须同时设置android:singleLine=”true”,因为输入法键盘右下角默认的回车键本来就是换行用的,当设置单行后,回车换行就失去作用了,这样就可以设置为搜索、发送、go等等。或是设置android:inputType

设置了android:imeOptions属性以后可以设置ActionListener来监听回车键的事件,以便进行相应的处理

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {//搜索

                    KeyText =  editText.getText().toString().trim();
                    return true;
                }
                return false;
            }
        });
  •  

EditText还可以设置监听输入内容的变化

 editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
               //输入前
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //输入中
            }

            @Override
            public void afterTextChanged(Editable editable) {
                //输入内容结束以后可以做一些相应的处理
                if ("".equals(editText.getText().toString().trim())) {

                }
            }
        });

延时自动弹出软件盘

  • 需要用到InputMethodManager
public static void showSoftKeyboardLate(final View view) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

            Context context = view.getContext();
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            view.requestFocus();
            imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
            }
        }, 998);
    }
  • 控制软件盘的显示和隐藏
public static void toggleSoftKeyboard(Context context, View view, boolean isShow) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (isShow) {
            view.requestFocus();
            imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
        } else {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

有时候刚打开activity界面,不需要弹出键盘,也可以在XML文件下EditText的父控件中添加:

  1. android:focusable="true"

  2. android:focusableInTouchMode="true"

软键盘弹出详细原理见:博客地址:http://blog.csdn.net/l540675759/article/details/74528641

猜你喜欢

转载自blog.csdn.net/kdsde/article/details/81317078