3

我使用以下代码显示键盘

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
        InputMethodManager.HIDE_IMPLICIT_ONLY);

我使用以下代码隐藏键盘getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

隐藏键盘,以及

也为此尝试过

InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

但是按home键后。应用程序关闭,但屏幕上的键盘保持不变。

请指导我。我究竟做错了什么?

我已经把我的隐藏代码。我把 hide 放进去onDestroy()onBackPressed()onOptionsItemSelected(MenuItem item) 对不起我的代码格式。

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);

    textTv=(EditText)findViewById(R.id.textview1);
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
    InputMethodManager.HIDE_IMPLICIT_ONLY);

    if(getIntent().getExtras()!=null)
    {
        Bundle extra=getIntent().getExtras();
        if(extra!=null)
        {
            // code
        }
    }
}

public void onBackPressed() {
    // TODO Auto-generated method stub
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(noteTv.getWindowToken(), 0);
    }

public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    switch (item.getItemId()) {
    case R.id.menu1:
    //code
    break;
    case R.id.menu2:
    //code
    break;

}

4

1 回答 1

8

尝试

@Override
protected void onPause() {
    final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(noteTv.getWindowToken(), 0);
    super.onPause();
}
于 2013-03-16T10:09:25.693 回答