我在操作栏中使用搜索字段,将其设置为名为search_layout.xml的布局文件夹中的xml。在2.3.3版本的android中,有一个问题,如搜索图标未显示在键盘中我输入文字后的第一次。相反,我必须单击“输入”键,然后单击文本字段,然后只显示“搜索”图标。这不会发生在更高版本,如4.0.4和4.4.2的android。
这是search_layout.xml:
此xml是参考res>> menu>> menu.xml
<item
android:id="@+id/action_notification"
android:actionLayout="@layout/search_layout"
android:icon="@drawable/ic_searchicon"
android:orderInCategory="0"
android:showAsAction="always|collapseActionView"
android:title="Search"/>
这是我在名为Activity_HomeScreen类的活动中使用的代码片段。
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem itemListMap,itemRefresh;
editsearch = (EditText) menu.findItem(R.id.action_notification).getActionView();
editsearch.addTextChangedListener(textWatcher);
editsearch.setOnEditorActionListener(new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (actionId == EditorInfo.IME_ACTION_SEARCH)
{
editsearch.clearFocus();
InputMethodManager imm = (InputMethodManager)Activity_HomeScreen.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editsearch.getWindowToken(), 0);
captureViepagerFragments(mVpContainer.getCurrentItem());
}
return false;
}
});
MenuItem menuSearch = menu.findItem(R.id.action_notification);
menuSearch.setOnActionExpandListener(new OnActionExpandListener()
{
// Menu Action Collapse
@Override
public boolean onMenuItemActionCollapse(MenuItem item)
{
// Empty EditText to remove text filtering
editsearch.setText("");
editsearch.clearFocus();
InputMethodManager imm = (InputMethodManager)Activity_HomeScreen.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editsearch.getWindowToken(), 0);
captureViepagerFragments(mVpContainer.getCurrentItem());
return true;
}
// Menu Action Expand
@Override
public boolean onMenuItemActionExpand(MenuItem item)
{
// Focus on EditText
editsearch.requestFocus();
// Force the keyboard to show on EditText focus
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
答案 0 :(得分:0)
通过这样做解决了这个问题:
public boolean onMenuItemActionExpand(MenuItem item)
{
editsearch.clearFocus();
editsearch.post(new Runnable() {
@Override
public void run() {
editsearch.requestFocus();
final InputMethodManager imm = (InputMethodManager) Activity_Cat_Products.this
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editsearch, InputMethodManager.SHOW_IMPLICIT);
}
});
return true;
}