5

是否可以防止用户关闭软键盘。换句话说,当我的活动可见时,屏幕上的键盘(始终)可用。这可能吗?如果是这样,你将如何实现它?

4

2 回答 2

1

无论焦点如何,这都应该强制键盘显示:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourViewObject.getWindowToken(), 0);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
于 2016-12-14T12:32:39.797 回答
-1

您需要覆盖onKeyUp()onKeyDown()方法,如下所示,

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK)     
    {
        ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput
                (InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
    }
    return super.onKeyUp(keyCode, event);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK)     
    {
        ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput
                (InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }
    return super.onKeyDown(keyCode, event);
}

您也可以尝试任何一种方式,通过使用OnEditorActionListener,看看下面的代码,

EditText txtMyEdit = (EditText) findViewById(R.id.txtEdit);
txtMyEdit.setOnEditorActionListener(new OnEditorActionListener() 
{
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) 
    {
        if (actionId == EditorInfo.IME_ACTION_DONE) 
        {
            // your additional processing... 
            return true;
        } 
        else 
        {
            return false;
        }
    }
});
于 2012-10-23T01:57:26.873 回答