方法运行时禁用键盘

时间:2013-07-05 09:42:59

标签: java android

如何在我的方法运行时禁用键盘并在完成后再次启用。这是我的代码

public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_UP) {
                    try {
        iKey = Integer.parseInt(eLocation.getText().toString());
                    } catch (Exception e) {
                        eLocation.setText("");
                        return false;
                    }

                    switch (iKey) {
                    case 1:
                        mymethod(1);
                    break;
                    case 2:
                        mymethod(2);
                    break;
}

1 个答案:

答案 0 :(得分:1)

隐藏键盘使用:

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);

显示键盘用途:

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// only will trigger it if no physical keyboard is open
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

如果您在活动之外使用此代码,请将行更改为:

 InputMethodManager mgr = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
相关问题