content: package com.liyi.sutils.utils;
  
  
  import android.app.Activity;
  import android.content.Context;
  import android.support.annotation.NonNull;
  import android.support.annotation.Nullable;
  import android.view.View;
  import android.view.WindowManager;
  import android.view.inputmethod.InputMethodManager;
  
  /**
   * 键盘相关工具类
   */
  public final class KeyboardUtil {
  
      private KeyboardUtil() {
          throw new UnsupportedOperationException("cannot be instantiated");
      }
  
      /*
        避免输入法面板遮挡
        <p>在 manifest.xml 中 activity 中设置</p>
        <p>android:windowSoftInputMode="adjustPan"</p>
       */
  
      /**
       * 动态显示软键盘
       *
       * @param activity activity
       */
      public static void showSoftInput(@Nullable Activity activity) {
          View view = activity.getCurrentFocus();
          if (view == null) view = new View(activity);
          InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
          if (imm == null) return;
          imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
      }
  
      /**
       * 动态显示软键盘
       *
       * @param view 视图
       */
      public static void showSoftInput(@Nullable View view) {
          view.setFocusable(true);
          view.setFocusableInTouchMode(true);
          view.requestFocus();
          InputMethodManager imm = (InputMethodManager) SUtils.getApp().getSystemService(Context.INPUT_METHOD_SERVICE);
          if (imm == null) return;
          imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
      }
  
      /**
       * 动态显示软键盘并把布局顶上去
       *
       * @param context 上下文对象
       * @param view    视图对象
       */
      public static void showSoftInputTop(@NonNull Context context, View view) {
          ((Activity) context).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
          showSoftInput(view);
      }
  
      /**
       * 动态隐藏软键盘
       *
       * @param activity activity
       */
      public static void hideSoftInput(@Nullable Activity activity) {
          View view = activity.getCurrentFocus();
          if (view == null) view = new View(activity);
          InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
          if (imm == null) return;
          imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
      }
  
      /**
       * 动态隐藏软键盘
       *
       * @param view 视图
       */
      public static void hideSoftInput(@Nullable View view) {
          InputMethodManager imm = (InputMethodManager) SUtils.getApp().getSystemService(Context.INPUT_METHOD_SERVICE);
          if (imm == null) return;
          imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
      }
  
      /**
       * 判断软件盘是否弹出
       *
       * @param activity
       * @return
       */
      public static boolean isSoftInputShow(@NonNull Activity activity) {
          View view = activity.getWindow().peekDecorView();
          if (view != null) {
              InputMethodManager inputmanger = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
              return inputmanger.isActive() && activity.getWindow().getCurrentFocus() != null;
          }
          return false;
      }
  
      /**
       * 切换键盘显示与否的状态
       * <p>如果键盘在窗口上已经显示，则隐藏，反之则显示</p>
       */
      public static void toggleSoftInput() {
          InputMethodManager imm = (InputMethodManager) SUtils.getApp().getSystemService(Context.INPUT_METHOD_SERVICE);
          if (imm == null) return;
          imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
      }
  }
  