Skip to content

Instantly share code, notes, and snippets.

@beilly
Last active April 25, 2016 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beilly/132c980ef1b744e8a1989ff1e461f4bb to your computer and use it in GitHub Desktop.
Save beilly/132c980ef1b744e8a1989ff1e461f4bb to your computer and use it in GitHub Desktop.
键盘管理器
package com.benli.app.utils;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by shibenli on 2016/3/23.
*/
public class InputTools {
// 切换虚拟键盘状态
public static void ChangeKeyboardStatus(Context context) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
// 隐藏虚拟键盘
public static void HideKeyboard(View v) {
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
}
}
// 显示虚拟键盘
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 KeyBoard(final EditText txtSearchKey, final String status) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
InputMethodManager m = (InputMethodManager) txtSearchKey.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (status.equals("open")) {
m.showSoftInput(txtSearchKey, InputMethodManager.SHOW_FORCED);
} else {
m.hideSoftInputFromWindow(txtSearchKey.getWindowToken(), 0);
}
}
}, 300);
}
// 通过定时器强制隐藏虚拟键盘
public static void TimerHideKeyboard(final View v) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
}
}
}, 10);
}
// 输入法是否显示着
public static boolean KeyBoard(EditText edittext) {
boolean bool = false;
InputMethodManager imm = (InputMethodManager) edittext.getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
bool = true;
}
return bool;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment