1 package com.xdsjs.save.utils;
2
3 import android.content.Context;
4 import android.view.inputmethod.InputMethodManager;
5 import android.widget.EditText;
6
7 import java.util.Timer;
8 import java.util.TimerTask;
9
10 /**
11 * 打开或关闭软键盘
12 *
13 * @author xdsjs
14 */
15 public class KeyBoardUtils {
16 /**
17 * 打卡软键盘
18 *
19 * @param mEditText 输入框
20 * @param mContext 上下文
21 */
22 public static void openKeybord(final EditText mEditText, final Context mContext) {
23
24 //必须要等UI绘制完成之后,打开软键盘的代码才能生效,所以要设置一个延时
25 Timer timer = new Timer();
26 timer.schedule(new TimerTask() {
27 @Override
28 public void run() {
29 InputMethodManager imm = (InputMethodManager) mContext
30 .getSystemService(Context.INPUT_METHOD_SERVICE);
31 imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
32 imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
33 InputMethodManager.HIDE_IMPLICIT_ONLY);
34 }
35 }, 500);
36 }
37
38 /**
39 * 关闭软键盘
40 *
41 * @param mEditText 输入框
42 * @param mContext 上下文
43 */
44 public static void closeKeybord(EditText mEditText, Context mContext) {
45 InputMethodManager imm = (InputMethodManager) mContext
46 .getSystemService(Context.INPUT_METHOD_SERVICE);
47
48 imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
49 }
50 }