300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Android 显示软键盘输入法和强制隐藏软键盘输入法

Android 显示软键盘输入法和强制隐藏软键盘输入法

时间:2018-09-12 05:40:33

相关推荐

Android 显示软键盘输入法和强制隐藏软键盘输入法

1.写一个软键盘输入法管理类。

package mon_library.manager;import android.app.Activity;import android.content.Context;import android.graphics.Rect;import android.view.View;import android.view.ViewTreeObserver;import android.view.inputmethod.InputMethodManager;import android.widget.EditText;import mon_library.callback.OnSoftKeyBoardChangeListener;import java.util.List;/*** author : Urasaki* e-mail : 1164688204@* date: /3/11 9:23* introduce : 软键盘管理类*/public class SoftKeyboardManager {private static final String TAG = SoftKeyboardManager.class.getSimpleName();private View rootView;//activity的根视图private int rootViewVisibleHeight;//纪录根视图的显示高度private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;public SoftKeyboardManager(Activity activity) {//获取activity的根视图rootView = activity.getWindow().getDecorView();//监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {//获取当前根视图在屏幕上显示的大小Rect r = new Rect();rootView.getWindowVisibleDisplayFrame(r);int visibleHeight = r.height();System.out.println(""+visibleHeight);if (rootViewVisibleHeight == 0) {rootViewVisibleHeight = visibleHeight;return;}//根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变if (rootViewVisibleHeight == visibleHeight) {return;}//根视图显示高度变小超过200,可以看作软键盘显示了if (rootViewVisibleHeight - visibleHeight > 200) {if (onSoftKeyBoardChangeListener != null) {onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);}rootViewVisibleHeight = visibleHeight;return;}//根视图显示高度变大超过200,可以看作软键盘隐藏了if (visibleHeight - rootViewVisibleHeight > 200) {if (onSoftKeyBoardChangeListener != null) {onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);}rootViewVisibleHeight = visibleHeight;return;}}});}private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;}public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {SoftKeyboardManager softKeyBoardListener = new SoftKeyboardManager(activity);softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);}/*** 显示软键盘(输入法)(可用于Activity,Fragment)** @param activity* @param editText*/public static void showInputMethod(final Activity activity, final EditText editText) {InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);}/*** 隐藏软键盘(输入法)(可用于Activity,Fragment)** @param activity*/public static void hideInputMethod(final Activity activity) {InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);if (inputMethodManager.isActive() && activity.getCurrentFocus() != null) {if (activity.getCurrentFocus().getWindowToken() != null) {inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);}}}/*** 显示软键盘(输入法)(只适用于Activity,不适用于Fragment)*/public static void showSoftKeyboard2(Activity activity) {View view = activity.getCurrentFocus();if (view != null) {InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);if (inputMethodManager != null) {inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);}}}/*** 隐藏软键盘(输入法)(只适用于Activity,不适用于Fragment)*/public static void hideSoftKeyboard(Activity activity) {View view = activity.getCurrentFocus();if (view != null) {InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);if (inputMethodManager != null) {inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);}}}/*** 隐藏软键盘(输入法)(可用于Activity,Fragment)*/public static void hideSoftKeyboard(Context context, List<View> viewList) {if (viewList == null) return;InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);for (View v : viewList) {if (inputMethodManager != null) {inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);}}}}

2.显示输入法。

//初始化EditTextEditText edtInput = (EditText) findViewById(R.id.edt_input);//显示输入法(可用于Activity,Fragment)SoftKeyboardManager.showInputMethod(rxAppCompatActivity, edtInput);

3.强制隐藏输入法。

//初始化EditTextEditText edtInput = (EditText) findViewById(R.id.edt_input);//强制隐藏输入法(可用于Activity,Fragment)SoftKeyboardManager.hideInputMethod(this);

如对此有疑问,请联系qq1164688204。

推荐Android开源项目

项目功能介绍:RxJava2和Retrofit2项目,添加自动管理token功能,添加RxJava2生命周期管理,使用App架构设计是MVP模式和MVVM模式,同时使用组件化,部分代码使用Kotlin,此项目持续维护中。

项目地址:/urasaki/RxJava2AndRetrofit2

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。