我有一个EditText和按钮对齐到父母的底部。
当我在其中输入文本并按下按钮保存数据时,虚拟键盘不会消失。
谁能指导我如何隐藏键盘?
我有一个EditText和按钮对齐到父母的底部。
当我在其中输入文本并按下按钮保存数据时,虚拟键盘不会消失。
谁能指导我如何隐藏键盘?
Answers:
这应该工作。
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS); 
只要确保this.getCurrentFocus()不返回null,如果没有焦点,它将返回null。
您可能还需要在EditText中定义imeOptions。这样,一旦您按“完成”,键盘就会消失:
<EditText
    android:id="@+id/editText1"
    android:inputType="text"
    android:imeOptions="actionDone"/>
    onEditorAction([...])。返回true会阻止键盘正确隐藏。
                    android:singleLine="true"。
                    android:singleLine="true"已弃用,无法使用maxLines=1。有任何想法吗?
                    singleLine已替换为maxLines=1。应该按预期工作。如果不是,请创建一个单独的问题。
                       mEtNumber.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    // do something, e.g. set your TextView here via .setText()
                    InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    return true;
                }
                return false;
            }
        });
并在xml中
  android:imeOptions="actionDone"
    我没有看到任何人使用此方法:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean focused) {
        InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (focused)
            keyboard.showSoftInput(editText, 0);
        else
            keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }
});
然后只请求将焦点放在editText上:
editText.requestFocus();
    EditText操作侦听器中包含的解决方案:
public void onCreate(Bundle savedInstanceState) {
    ...
    ...
    edittext = (EditText) findViewById(R.id.EditText01);
    edittext.setOnEditorActionListener(new OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                in.hideSoftInputFromWindow(edittext.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
            }
            return false;
        }
    });
    ...
    ...
}
    我发现这是因为我的EditText在进入时并没有自动被解雇。
这是我的原始代码。
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if ( (actionId == EditorInfo.IME_ACTION_DONE) || ((event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN ))) {
            // Do stuff when user presses enter
            return true;
        }
        return false;
    }
});
我通过删除线解决了它
return true;
在用户按回车键完成操作后。
希望这对某人有帮助。
在过去的几天里一直在为此苦苦挣扎,并找到了一个非常有效的解决方案。当在EditText之外的任何地方进行触摸时,软键盘将被隐藏。
代码发布在这里:在android中单击时隐藏默认键盘
您可以像这样轻松地为呼叫创建单例类:
public class KeyboardUtils {
    private static KeyboardUtils instance;
    private InputMethodManager inputMethodManager;
    private KeyboardUtils() {
    }
    public static KeyboardUtils getInstance() {
        if (instance == null)
            instance = new KeyboardUtils();
        return instance;
    }
    private InputMethodManager getInputMethodManager() {
        if (inputMethodManager == null)
            inputMethodManager = (InputMethodManager) Application.getInstance().getSystemService(Activity.INPUT_METHOD_SERVICE);
        return inputMethodManager;
    }
    @SuppressWarnings("ConstantConditions")
    public void hide(final Activity activity) {
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                try {
                    getInputMethodManager().hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
这样,以后可以调用活动下一个表格了:
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
        KeyboardUtils.getInstance().hide(this);
    }
}
    您可以在顶部看到标记的答案。但是我用getDialog().getCurrentFocus()得很好并且运行良好。我发布此答案是因为我无法输入"this"oncreatedialog。
这就是我的答案。如果您尝试了带标记的答案但不起作用,则可以尝试以下操作:
InputMethodManager inputManager = (InputMethodManager) getActivity().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getDialog().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    我使用此方法从编辑文本中删除键盘:
 public static void hideKeyboard(Activity activity, IBinder binder) {
    if (activity != null) {
        InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (binder != null && inputManager != null) {
            inputManager.hideSoftInputFromWindow(binder, 0);//HIDE_NOT_ALWAYS
            inputManager.showSoftInputFromInputMethod(binder, 0);
        }
    }
}
而且这种将键盘从活动中移除的方法(在某些情况下不起作用,例如,当edittext绑定到键盘时,失去焦点,它将无法工作。但是在其他情况下,它会很好用,并且您没有关心容纳键盘的元素)。
 public static void hideKeyboard(Activity activity) {
    if (activity != null) {
        InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (activity.getCurrentFocus() != null && inputManager != null) {
            inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
            inputManager.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
        }
    }
}
    int klavStat = 1; // for keyboard soft/hide button
int inType;  // to remeber your default keybort Type
编辑器-是EditText字段
/// metod for onclick button ///
public void keyboard(View view) {
    if (klavStat == 1) {
        klavStat = 0;
        
        inType = editor.getInputType();
        
        InputMethodManager imm = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
     
        editor.setInputType(InputType.TYPE_NULL);
        editor.setTextIsSelectable(true);
    } else {
        klavStat = 1;
        InputMethodManager imm = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
        editor.setInputType(inType);
    }
}
如果您有另一个EditText字段,则需要注意焦点更改。
就我而言,为了在按下“发送按钮”时隐藏键盘,我使用了可接受的答案,但是将上下文更改为getApplication并添加了getWindow()。
InputMethodManager inputManager = (InputMethodManager) getApplication().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    editText.setInputType(InputType.TYPE_NULL);