package cn.bingoogolapple.weibo.util; import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.os.Handler; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; /** * 作者:王浩 邮件:bingoogolapple@gmail.com * 创建时间:15/7/3 上午12:57 * 描述: */ public class KeyboardUtils { private KeyboardUtils() { } /** * 关闭activity中打开的键盘 * @param activity */ public static void closeKeyboard(Activity activity) { View view = activity.getWindow().peekDecorView(); if (view != null) { InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); } } /** * 关闭dialog中打开的键盘 * @param dialog */ public static void closeKeyboard(Dialog dialog) { View view = dialog.getWindow().peekDecorView(); if (view != null) { InputMethodManager inputMethodManager = (InputMethodManager) dialog.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); } } /** * 打开键盘 * @param context * @param editText */ public static void openKeyboard(final Context context, final EditText editText) { new Handler().postDelayed(new Runnable() { @Override public void run() { editText.requestFocus(); editText.setSelection(editText.getText().toString().length()); InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED); } }, 300); } }