Android - EditText 不会让键盘消失



我有一个EditText,我需要它失去焦点当用户按下键盘上的"完成"时使键盘消失。我的代码如下:

etFromCustom.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            ((LinearLayout) findViewById(R.id.linlay_dummy)).requestFocus(); //this is what I have to do - send focus to a dummy layout
            return true;
        }               
        return false;
    }
}); 

我知道,默认情况下,键盘上的"完成"键会隐藏软件键盘,但由于我覆盖了它,它不再工作:EditText确实失去了焦点,但键盘非但没有消失,反而从数字键变成了qwerty键。

试试这个,

将此代码添加到xml中。xml文件中的特定EditText

android:imeOptions="actionDone"

尝试以下代码

放入创建活动

private InputMethodManager imm;
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

DoneButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {
            //  yourEditTextView.setText("");
                imm.hideSoftInputFromWindow(
                        searchEditTextView.getWindowToken(), 0);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

最新更新