【问题标题】:Android Studio - Input Method ManagerAndroid Studio - 输入法管理器
【发布时间】:2018-02-03 01:42:55
【问题描述】:

Android工作室版本:2.3.3 下面的代码不起作用,它应该隐藏键盘但它不是。请帮助。

 InputMethodManager imm = (InputMethodManager) 
    getSystemService(Context.INPUT_METHOD_SERVICE);
    public void setImm(InputMethodManager imm) {
    this.imm = imm;
    }
    public InputMethodManager getImm() {
    imm.hideSoftInputFromWindow(urledit.getWindowToken(),0);
    return imm;
    }

【问题讨论】:

  • 嗨,欢迎来到堆栈溢出。请参阅How to Ask 链接以获取有关如何提出问题并相应更新您的问题的更多详细信息。

标签: android android-studio webview inputmethodmanager


【解决方案1】:
/**
 * Hides the soft keyboard
 */
 public void hideSoftKeyboard() {
   if(getCurrentFocus()!=null) {
   InputMethodManager inputMethodManager = (InputMethodManager) 
 getSystemService(INPUT_METHOD_SERVICE);
   inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
  }
}

 /**
 * Shows the soft keyboard
 */
public void showSoftKeyboard(View view) {
   InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
   view.requestFocus();
   inputMethodManager.showSoftInput(view, 0);
}

试试这个隐藏和显示键盘

【讨论】:

    【解决方案2】:

    希望有用

    public static void hideKeyboard(Activity activity) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    View view = activity.getCurrentFocus();
    if (view == null) {
        view = new View(activity);
    }
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    

    【讨论】:

      【解决方案3】:

      您好,使用此代码,它可以 100% 工作

                  View.OnTouchListener disable = new View.OnTouchListener() {
                  public boolean onTouch (View v, MotionEvent event) {
                  v.onTouchEvent(event);
                  InputMethodManager img = 
                 (InputMethodManager)v.getContext().getSystemService(INPUT_METHOD_SERVICE);
                  if (img != null) {
                  img.hideSoftInputFromWindow(v.getWindowToken(), 0);
                  }
                  System.out.println(ee.getSelectionEnd());
                  return true;
                 }
                 };
                 //create a Button to setOnTouchListener(event)
                 Button ee=new Button(this);
                 ee.setOnTouchListener(disable);
      

      【讨论】:

        猜你喜欢
        • 2021-09-23
        • 2017-07-27
        • 1970-01-01
        • 2014-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多