最近遇到了關于軟鍵盤的問題,需要獲取到軟鍵盤的狀態,是否在顯示 ,記錄一下,方便以后查閱。網上常見的判定狀態方法
來判斷軟鍵盤是否打開,若相等則為打開。試了之后,發現這個只對手機自帶的鍵盤有作用,對安裝的第三方的輸入法沒有效果。
還有介紹使用InputMethodManager 來獲取鍵盤狀態,代碼如下
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); boolean isOpen=imm.isActive();//isOpen若返回true,則表示輸入法打開
這種并不能實時獲取到鍵盤的狀態,對我依然沒有效果。
后來找到的解決方法,監聽屏幕的變化,代碼如下:
import android.app.Activity;import android.content.Context;import android.graphics.Rect;import android.os.Build;import android.util.Log;import android.util.TypedValue;import android.view.View;import android.view.ViewGroup;import android.view.ViewTreeObserver;/** * * 軟鍵盤的監聽 */public class KeyBoardShowListener { private Context ctx; public KeyBoardShowListener(Context ctx) { this.ctx = ctx; } OnKeyboardVisibilityListener keyboardListener; public OnKeyboardVisibilityListener getKeyboardListener() { return keyboardListener; } public interface OnKeyboardVisibilityListener { void onVisibilityChanged(boolean visible); } public void setKeyboardListener(final OnKeyboardVisibilityListener listener, Activity activity) { final View activityRootView = ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0); activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { private boolean wasOpened; private final int DefaultKeyboardDP = 100; // From @nathanielwolf answer... Lollipop includes button bar in the root. Add height of button bar (48dp) to maxDiff private final int EstimatedKeyboardDP = DefaultKeyboardDP + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0); private final Rect r = new Rect(); @Override public void onGlobalLayout() { // Convert the dp to pixels. int estimatedKeyboardHeight = (int) TypedValue .applyDimension(TypedValue.COMPLEX_UNIT_DIP, EstimatedKeyboardDP, activityRootView.getResources().getDisplayMetrics()); // Conclude whether the keyboard is shown or not. activityRootView.getWindowVisibleDisplayFrame(r); int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top); boolean isShown = heightDiff >= estimatedKeyboardHeight; if (isShown == wasOpened) { Log.e("Keyboard state", "Ignoring global layout change..."); return; } wasOpened = isShown; listener.onVisibilityChanged(isShown); } }); }}用法如下:
//監聽軟鍵盤的狀態new KeyBoardShowListener(Activity.this).setKeyboardListener( new KeyBoardShowListener.OnKeyboardVisibilityListener() { @Override public void onVisibilityChanged(boolean visible) { if (visible) { //軟鍵盤已彈出 } else { //軟鍵盤未彈出 } } }, Activity.this);以下是可能會遇到的一些情況:
綁定軟鍵盤到EditText
edit.setFocusable(true);edit.setFocusableInTouchMode(true);edit.requestFocus();InputMethodManager inputManager = (InputMethodManager)edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);inputManager.showSoftInput(edit, 0);
去除軟鍵盤顯示:
editMsgView.setText("");editMsgView.clearFocus();//close InputMethodManagerInputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);imm.hideSoftInputFromWindow(editMsgView.getWindowToken(), 0);始終不彈出軟件鍵盤
也可以:
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);if(imm.isActive()){ //這里可以判斷也可以不判斷imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0 );}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答