Android - 焦点时隐藏EditText的键盘输入(API> = 17)

时间:2016-04-15 10:09:27

标签: android

我尝试了很多方法,但没有一种方法可行或可能用于API< 17.有人可以告诉我如何在焦点或单击EditText时隐藏键盘吗?

ViewGroup group = (ViewGroup)findViewById(R.id.f1entrygroup);
        for (int i = 0, count = group.getChildCount(); i < count; ++i) {
            View view = group.getChildAt(i);
            if (view instanceof EditText) {
                view.setOnFocusChangeListener(focusListener);
                view.setOnClickListener(entryClickListener);

                ***//HOW TO DISABLE THE KEYBOARD POP UP AT HERE?***

            }
        }

由于

3 个答案:

答案 0 :(得分:1)

在onCreate()方法

中设置布局后,请尝试以下代码
EditText edtView=(EditText)findViewById(R.id.editText);
edtView.setInputType(0);

触摸或对焦时,SoftInputKeyboard不会弹出。希望它有所帮助

答案 1 :(得分:0)

您可以通过调用此方法隐藏软键盘

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

答案 2 :(得分:0)

View.OnFocusChangeListener focusListener = (v, hasFocus) -> {
       if (hasFocus) {
           InputMethodManager inputMethodManager = (InputMethodManager) context
                   .getSystemService(Activity.INPUT_METHOD_SERVICE);
           inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
       }
};