Android中软键盘InputMethodManager的弹出和隐藏,以及显示和隐藏的监听

1.首先设置软键盘的弹出模式,设置在初始化View的前面

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        //键盘弹出方式Resize会挤压布局并重新计算布局,对权重布局进行压缩,有利于获取软键盘的显示和隐藏的监听,从而在监听中改变布局
//SOFT_INPUT_STATE_HIDDEN 为初始隐藏,点击EditText才弹出来        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);       
    }

// WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN
//pan模式不会挤压改变布局,直接从底部弹出,这样就不好拿到键盘显示和隐藏的监听,也可以获取键盘的高度

 getWindow().getDecorView().getViewTreeObserver().
                addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {

                        Rect r = new Rect();
                        getWindow().getDecorView().getWindowVisibleDisplayFrame(r);

                        int screenHeight = getWindow().getDecorView().getRootView().getHeight();
                        int heightDifference = screenHeight - (r.bottom - r.top);
                        // 保存通知栏高度
                        if (300 > heightDifference) {//隐藏键盘
                            SharedPreferenceUtil.saveDeviceNotificationBarHeight(getApplicationContext(), heightDifference);
                        }
                        // 保存键盘高度
                        else {//弹出键盘
                            int notificationBarHeight = SharedPreferenceUtil.getDeviceNotificationBarHeight(getApplicationContext());
                            if (-1 != notificationBarHeight) {
                                int softKeyBoardHeight = heightDifference - notificationBarHeight;
                                if (softKeyBoardHeight == SharedPreferenceUtil.getDeviceSoftKeyBoardHeight(getApplicationContext())) {
                                    return;
                                }
                                // 高度发生变化,保存新的软键盘高度,并更改底部编辑器的高度
                                SharedPreferenceUtil.saveDeviceSoftKeyBoardHeight(getApplicationContext(), softKeyBoardHeight);
                                if (View.VISIBLE == mEditorLayout.getVisibility()) {
                                    ViewGroup.LayoutParams layoutParams = mLvTexts.getLayoutParams();
                                    layoutParams.height = softKeyBoardHeight;
                                    mLvTexts.setLayoutParams(layoutParams);
                                }
                            }
                        }
                    }
                });

2.获取InputMethodManager

 mInputManager = (InputMethodManager) mEt.getContext().getSystemService(
                Context.INPUT_METHOD_SERVICE);

3.自定义一个ViewGroup作为跟布局,并复写大小改变的监听

/**
 * 监听onSizeChange的LinearLayout
 * Created by yj on 2015/10/26.
 */
public class PolaroidResizeLinearLayout extends LinearLayout {
    public PolaroidResizeLinearLayout(Context context) {
        super(context);
    }

    public PolaroidResizeLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PolaroidResizeLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    //大小改变的监听
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
        if (mListener != null)
        {
            mListener.OnResizeRelative(w, h, oldw, oldh);
        }
    }

    // 监听接口
    private OnResizeRelativeListener mListener;

    public interface OnResizeRelativeListener
    {
        void OnResizeRelative(int w, int h, int oldw, int oldh);
    }

    public void setOnResizeRelativeListener(OnResizeRelativeListener l)
    {
        mListener = l;
    }
}

4.将上述ViewGroup作为布局的跟布局,布局中结构为:

  <PolaroidResizeLinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#5500ff00"
        >
        ....
        </ScrollView>
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
        <!-- 或者wrap_content,这样才会挤压上方的ScrollView-->
            >
            ...
            </FrameLayout>

    </PolaroidResizeLinearLayout>

5.在代码中设置PolaroidResizeLinearLayout大小改变的监听,在监听中判断键盘是隐藏还是显示

  //跟布局对键盘弹出和隐藏的监听
        mResizeLl.setOnResizeRelativeListener(new PolaroidResizeLinearLayout.OnResizeRelativeListener() {
            @Override
            public void OnResizeRelative(int w, int h, int oldw, int oldh) {
                PolaroidLogUtils.d(TAG, "w,h,oldw,oldh:" + w + "--" + h + "--" + oldw + "--" + oldh);
                int dh = h - oldh;
                PolaroidLogUtils.d(TAG, "dh:" + dh);
                /*布局第一次inflate时,是由0变到填充界面的,所以这里要加上oldh>0,这样后面的改变都是键盘弹出和隐藏而带来,如果漆面键盘的弹出模式设置为WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN,则这个监听不会起作用,因为没有挤压布局,就不会有sizeChange*/
                if (oldh > 0) {
                    if (dh > 0) {
                     //TODO:do sth;
                        mInputMethodShowing = false;
                    } else if (dh < 0) {

                        mInputMethodShowing = true;
                        PolaroidLogUtils.d(TAG, "显示输入法");
                        boolean etFocuse = mEt.isFocused();
                        boolean editStampFoucse = mStampFragment.getEditText().isFocused();
                        PolaroidLogUtils.d(TAG, "etFocuse:stampFocuse:" + etFocuse + "---" + editStampFoucse);
                        //可以根据不同位置的EditText获取了焦点(即键盘输入)展现不同的界面布局,
                        if (etFocuse) {//ScrollView中的EditTExt获取键盘的事件操作
                            setFLModifyLayoutHeight(mFontTabHeight);
                            showInputMethodFontFragment();
                        }
                    }
                }
            }
        });

6.还可以在不同的EditText中焦点切换时(键盘输入 从A到B TextView)显示不同的布局

mEt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    PolaroidLogUtils.d(TAG, "获取了焦点");
                    if (mInputMethodShowing) {
                        setFLModifyLayoutHeight(mFontTabHeight);
                        showInputMethodFontFragment();
                    }
                }
            }
        });

7.还可以在点击按钮时自己控制键盘的显示和隐藏

//显示
 mInputManager.showSoftInput(mEt, 0);
 //隐藏 和对应的显示是相对应的才有效,即这列的mEt要相同
 mInputManager.hideSoftInputFromWindow(mEt.getWindowToken(), 0);
//显示和隐藏的切换 (即隐藏点击显示,显示点击隐藏)
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值