package com.dh.foundation.utils;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import java.util.Timer;
import java.util.TimerTask;
/**
* 系统键盘管理者
* Created by Yang on 14-4-26.
*/
public class InputUtils {
/**
* 隐藏虚拟键盘
*
* @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 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;
}
/**
* 设置键盘隐藏机制<br/>
* 效果:点击输入框外的区域隐藏输入法软键盘<br>
* 前提,当前界面不能有控件设置onTouchListener
*/
public static void setupUISoftKeyBoardHideSystem(final View view) {
//Set up touch listener for non-text box views to hide keyboard.
if (!(view instanceof EditText)) {
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
hideKeyboard(v);
}
return false;
}
});
}
//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUISoftKeyBoardHideSystem(innerView);
}
}
}
}