如何处理硬件键盘进入视图?

时间:2012-03-25 20:11:58

标签: android android-view

我正在使用以下代码编程创建EditView:

    mSearchBar = new EditText(this);

    mSearchBar.setHint("Enter sim name.");

    mSearchBar.setLayoutParams(new LayoutParams(
                layoutWidth,
                LayoutParams.WRAP_CONTENT));

    mSearchBar.setOnEditorActionListener(mEditorAction);

    mSearchBar.setImeOptions(EditorInfo.IME_ACTION_SEARCH);

    mSearchBar.setInputType(
              InputType.TYPE_CLASS_TEXT
            | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
            );

然后我将mEditorAction与...

private OnEditorActionListener mEditorAction = new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId == EditorInfo.IME_ACTION_SEARCH) {
            gotoSim(v.getText().toString());
            InputMethodManager imm = (InputMethodManager)SLMapTest.this
                .getSystemService(SLMapTest.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            mSearch.collapseActionView();
            return true;
        }
        return false;
    }
};

然而,这个代码(我从谷歌搜索得到的)显然不适合硬件键盘。我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:3)

onKeyListener()附加到您的视图,然后附加到侦听器

密钥监听器中的

if(keyCode == KeyEvent.KEYCODE_ENTER) //do something