package com.warmlight.voicepacket.utils;
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
public class KeyboardUtils {
/**
* 是否有顯示鍵盤
*
* @param activity 活動
* @return 是否有顯示鍵盤
*/
public static boolean isSoftShowing(Activity activity) {
//獲取當前屏幕內容的高度
int screenHeight = activity.getWindow().getDecorView().getHeight();
//獲取View可見區域的bottom
Rect rect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
return screenHeight - rect.bottom != 0;
}
/**
* 判斷軟鍵盤是否彈出
*/
public static boolean isSHowKeyboard(Context context, View v) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(context.INPUT_METHOD_SERVICE);
if (imm.hideSoftInputFromWindow(v.getWindowToken(), 0)) {
imm.showSoftInput(v, 0);
return true;
//軟鍵盤已彈出
} else {
return false;
//軟鍵盤未彈出
}
}
/**
* 打卡軟鍵盤
*
* @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 v
*/
public static void HideKeyboard(View v) {
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
}
}
/***
* 顯示虛擬鍵盤
*
* @param v
*/
public static void ShowKeyboard(View v) {
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(v, InputMethodManager.SHOW_FORCED);
}
/*
* 打開輸入法面板
*/
public static void showInputMethod(final Activity activity){
if(activity == null)return;
InputMethodManager inputMethodManager = ((InputMethodManager)activity.getSystemService(
Activity.INPUT_METHOD_SERVICE));
if(activity.getCurrentFocus() != null){
inputMethodManager.showSoftInput(activity.getCurrentFocus(), 0);
}
}
/*
* 關閉輸入法面板
*/
public static void hideInputMethod(final Activity activity){
if(activity == null)return;
InputMethodManager inputMethodManager = ((InputMethodManager)activity.getSystemService(
Activity.INPUT_METHOD_SERVICE));
if(activity.getCurrentFocus() != null){
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus()
.getWindowToken(), 0);
}
}
/*
* 切換軟鍵盤顯示狀態
*/
public static void setInputMethodVisibility(Context context, EditText editText, boolean visible){
if (context == null || editText == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (visible) {
imm.toggleSoftInput(InputMethodManager.RESULT_UNCHANGED_SHOWN,
InputMethodManager.HIDE_NOT_ALWAYS);
} else {
imm.hideSoftInputFromWindow(editText.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
editText.setTag(visible);
}
}