我正在用这个代码创建一个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;
    }
};


但是,这段代码(我是从谷歌搜索得到的)显然不能与硬件键盘配合使用。我该怎么做才能解决此问题?

最佳答案

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

在关键听众
if(keyCode == KeyEvent.KEYCODE_ENTER)
//do something

10-08 17:11