简体   繁体   English

在Android中隐藏软键盘

[英]Hide soft keyboard in android

I want to hide keyboard after clicking on EditText in android i tried below code but its not working. 我想在Android中单击EditText后隐藏键盘,但尝试了以下代码,但无法正常工作。

 mPassword.setInputType(InputType.TYPE_CLASS_NUMBER);
    mPassword.requestFocus();
    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.showSoftInput(mPassword, InputMethodManager.RESULT_HIDDEN);

I have also given android:windowSoftInputMode="stateHidden" in activity manifest. 我还在活动清单中给出了android:windowSoftInputMode="stateHidden" still i'm getting keyboard.Please tell me how can i hide soft keyboard?? 还是我要键盘。请告诉我如何隐藏软键盘??

You can use the following code to hide the soft keyboard 您可以使用以下代码隐藏软键盘

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mPassword.getWindowToken(), 0);

Also, 也,

If you want to hide when the activity starts, then edit your manifest file as 如果您想在活动开始时隐藏,则将manifest文件编辑为

<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden"  />

Use following code in your manifest file. 在清单文件中使用以下代码。

<activity
     android:name="YourActivity"
     android:configChanges="keyboardHidden"
     android:windowSoftInputMode="stateHidden"/>

Try like this, 这样尝试

your_edittext.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                InputMethodManager m = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (m != null) {
                    m.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
                    your_edittext.clearFocus();
                }
            }
        });

Here's the solution which will hide the keyboard from anywhere. 这是可以在任何地方隐藏键盘的解决方案。

1st create in your activity of choice the listener for the state and the method that will do the closing (based on the open state). 1在您选择的活动中创建状态和将执行关闭操作的方法(基于打开状态)的侦听器。

public class MainActivity extends SherlockFragmentActivity {

  private boolean mKeyboardOpen = false;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "onCreate()");

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    // add "keyboard open listener"
    final View v = findViewById(R.id.pager);
    v.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
        int hRoot = v.getRootView().getHeight();
        int hView = v.getHeight();
        int heightDiff = hRoot - hView;
        // if more than 150 pixels, its probably a keyboard...
        mKeyboardOpen = heightDiff > 150;
        Log.d(TAG, "hRoot=" + hRoot + ", hView=" + hView + ", mKeyboardOpen=" + mKeyboardOpen);
      }
    });
  }


  public void closeSoftKeyboard() {
    if (mKeyboardOpen) {
      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    }
  }
}

2nd call ((MainActivity) getActivity()).closeSoftKeyboard(); 第二次调用((MainActivity) getActivity()).closeSoftKeyboard(); from anywhere, eg your EditText's OnClickListener() . 从任何地方,例如您的EditText的OnClickListener()

Hint: I'm using the ViewPager root view (R.id.pager), but you should probably replace it with your view root id. 提示:我正在使用ViewPager根视图(R.id.pager),但您可能应该用视图根ID替换它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM