package com.xdsjs.save.utils;

 import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText; import java.util.Timer;
import java.util.TimerTask; /**
* 打开或关闭软键盘
*
* @author xdsjs
*/
public class KeyBoardUtils {
/**
* 打卡软键盘
*
* @param mEditText 输入框
* @param mContext 上下文
*/
public static void openKeybord(final EditText mEditText, final Context mContext) { //必须要等UI绘制完成之后,打开软键盘的代码才能生效,所以要设置一个延时
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) mContext
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}, 500);
} /**
* 关闭软键盘
*
* @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);
}
}
05-12 18:31