Android - EditText不会让键盘消失

时间:2013-02-11 10:16:59

标签: android

我有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确实失去了焦点,而键盘则改为消失,只是从数字1变为qwerty。

2 个答案:

答案 0 :(得分:1)

试试这个,

在xml中添加此代码。您xml文件中的特定EditText

android:imeOptions="actionDone"

答案 1 :(得分:0)

尝试以下代码

投入创建活动

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();
            }
        }
    });
相关问题