軟鍵盤Edittext監聽開啟和關閉工具類
阿新 • • 發佈:2019-02-20
import android.app.Activity; import android.content.Context; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; public class KeyBordS { /** * 開啟軟鍵盤 * * @param mEditText * @param mContext */ public static void openKeybord(EditText mEditText,Context mContext) { InputMethodManager imm = (InputMethodManager) mContext .getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); } /** * 關閉軟鍵盤 * * @param mEditText輸入框 * @param mContext上下文 */ public static void closeKeybord(EditText mEditText, Context mContext) { InputMethodManager imm = (InputMethodManager) mContext .getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(mEditText.getWindowToken(),0); } /** * 判斷當前軟鍵盤是否開啟 * * @param activity * @return */ public static boolean isSoftInputShow(Activity activity) { // 虛擬鍵盤隱藏 判斷view是否為空 View view = activity.getWindow().peekDecorView(); if (view != null) { // 隱藏虛擬鍵盤 InputMethodManager inputmanger = (InputMethodManager) activity .getSystemService(Activity.INPUT_METHOD_SERVICE); // inputmanger.hideSoftInputFromWindow(view.getWindowToken(),0); return inputmanger.isActive() && activity.getWindow().getCurrentFocus() != null; } return false; } }