我正在使用以下代码编程创建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;
}
};
然而,这个代码(我从谷歌搜索得到的)显然不适合硬件键盘。我该怎么做才能解决这个问题?
答案 0 :(得分:3)
将onKeyListener()
附加到您的视图,然后附加到侦听器
if(keyCode == KeyEvent.KEYCODE_ENTER)
//do something