private void initSoftInputMethod() { this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); receiver = new switchInputMethodReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction("android.intent.action.INPUT_METHOD_CHANGED"); registerReceiver(receiver, filter); SystemConfigSp.instance().init(this); currentInputMethod = Settings.Secure.getString( MessageActivity.this.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); keyboardHeight = SystemConfigSp.instance().getIntConfig(currentInputMethod); }
@Override public void onGlobalLayout() { Rect r = new Rect(); baseRoot.getGlobalVisibleRect(r); // 进入Activity时会布局,第一次调用onGlobalLayout,先记录开始软键盘没有弹出时底部的位置 if (rootBottom == Integer.MIN_VALUE) { rootBottom = r.bottom; return; } // adjustResize,软键盘弹出后高度会变小 if (r.bottom < rootBottom) { // 按照键盘高度设置表情框和发送图片按钮框的高度 keyboardHeight = rootBottom - r.bottom; SystemConfigSp.instance().init(MessageActivity.this); SystemConfigSp.instance().setIntConfig(currentInputMethod, keyboardHeight); LayoutParams params = (LayoutParams) addOthersPanelView.getLayoutParams(); params.height = keyboardHeight; LayoutParams params1 = (LayoutParams) emoLayout.getLayoutParams(); params1.height = keyboardHeight; } }
@Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("android.intent.action.INPUT_METHOD_CHANGED")) { currentInputMethod = Settings.Secure.getString( MessageActivity.this.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); SystemConfigSp.instance() .setStrConfig(SystemConfigSp.SysCfgDimension.DEFAULTINPUTMETHOD, currentInputMethod); int height = SystemConfigSp.instance().getIntConfig(currentInputMethod); if (keyboardHeight != height) { keyboardHeight = height; addOthersPanelView.setVisibility(View.GONE); emoLayout.setVisibility(View.GONE); MessageActivity.this .getWindow() .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); messageEdt.requestFocus(); if (keyboardHeight != 0 && addOthersPanelView.getLayoutParams().height != keyboardHeight) { LayoutParams params = (LayoutParams) addOthersPanelView.getLayoutParams(); params.height = keyboardHeight; } if (keyboardHeight != 0 && emoLayout.getLayoutParams().height != keyboardHeight) { LayoutParams params = (LayoutParams) emoLayout.getLayoutParams(); params.height = keyboardHeight; } } else { addOthersPanelView.setVisibility(View.VISIBLE); emoLayout.setVisibility(View.VISIBLE); MessageActivity.this .getWindow() .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING); messageEdt.requestFocus(); } } }