我使用这段代码将可折叠的View (EditText)
放置在ActionBarSherlock
中:
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.activity_main, menu);
//Used to put dark icons on light action bar
boolean isLight = false;
menu.add("Search")
.setIcon(isLight ? R.drawable.ic_search_inverse : R.drawable.ic_search)
.setActionView(R.layout.collapsible_edittext)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return true;
}
当我试图在视图上设置setOnEditorActionListener
时(通过其ID),应用程序会崩溃:
EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch(v.getText().toString(), 3);
return true;
}
return false;
}
});
我需要在ActionBar
中加入一个SearchField
,并且在用户按下“搜索”键时执行搜索,但我不知道如何将setOnEditorActionListener
绑定到视图中。