简体   繁体   English

如果 Android 软键盘打开,则隐藏它

[英]Hide Android soft keyboard if it is open

I have three edit-text fields.我有三个编辑文本字段。 Of these fields I want to display the soft input keyboard only for the first field and disabled for the later two fields sice these are date and time fields.在这些字段中,我只想为第一个字段显示软输入键盘,并为后面的两个字段禁用这些是日期和时间字段。

Edit-Text 1 //Show the keyboard
Edit-Text 2 and 3 //Hide the keyboard

By using below code I'm able to disable the keyboard for field 2 and 3 but when the user has focus on field 1 the keyboard appears but didn't hide when user taps on field 2 or 3. Although when field 2 or 3 is tapped first no keyboard appears.通过使用下面的代码,我可以禁用字段 2 和 3 的键盘,但是当用户将焦点放在字段 1 上时,键盘出现但在用户点击字段 2 或 3 时没有隐藏。虽然当字段 2 或 3 是轻敲第一没有键盘出现。

//Code to disable soft input keyboard
public static void disableSoftInputFromAppearing(EditText editText) {
    if (Build.VERSION.SDK_INT >= 11) {
        editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
        editText.setTextIsSelectable(true);
    } else {
        editText.setRawInputType(InputType.TYPE_NULL);
        editText.setFocusable(true);
    }

How can I hide the soft input keyboard if its already open?如果软输入键盘已经打开,如何隐藏它?

// activity //活动

public static void hideSoftKeyboard(Activity activity) {

   InputMethodManager inputMethodManager = (InputMethodManager)activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
   inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
 }

// fragment //片段

public void hideSoftKeyboard() {
    InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}

// If edit-text loses the focus, hiding keyboard //如果编辑文本失去焦点,隐藏键盘

edTxtMessage.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (isEditable){
                v.setFocusable(true);
                v.setFocusableInTouchMode(true);
            } else {
                edTxtMessage.setFocusable(false);
            }

            return false;
        }
    });

edTxtMessage.setOnFocusChangeListener(new View.OnFocusChangeListener({
        @Override
        public void onFocusChange(View view, boolean b) {
            if (!b){
                hideKeyboard(getContext(), view);
            }
        }
    });

private void hideKeyboard(Context context, View view) {
    if (view != null) {
        InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

Easy way is using the xml简单的方法是使用xml

android:inputType="none"
android:textIsSelectable="true

You can set the two attributes to your edittext您可以将这两个属性设置为您的编辑文本

  android:focusable="false"
  android:focusableInTouchMode="false"

Note: By this Your edittext is clickable for the date and time not focus to softkeyboard注意:通过这个你的编辑文本是可点击的日期和时间而不是焦点到软键盘

Check if the current focus is not null, otherwise it might lead to a null pointer exception检查当前焦点是否不为空,否则可能导致空指针异常

if (getCurrentFocus() != null) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}

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

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