我正在使用ActionBarSherlock,在ActionBar中,当我点击searchIcon时,我会看到这个自定义布局。
public boolean onCreateOptionsMenu(final Menu menu) {
LayoutInflater inflater = getLayoutInflater();
final View view = inflater.inflate(R.layout.on_search_icon_clicked,null, false);
view.findViewById(R.id.search_image).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View arg0) {
EditText searchET = (EditText) view.findViewById(R.id.search_editText);
String s = searchET.getText().toString();
Toast.makeText(getBaseContext(), "Searching for: " + s,1000).show();
}
});
menu.add("Search")
.setIcon(R.drawable.ic_search)
.setActionView(view)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return true;
}
on_search_icon_clicked.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/search_image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:src="@drawable/ic_search"
android:clickable="true"
android:contentDescription="@string/seach_icon" />
<EditText
android:id="@+id/search_editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/search_image"
android:hint="@string/search"
android:singleLine="true" />
</RelativeLayout>
所以,i click on the SearchIcon
---&gt; i get the custom layout with editText and ImageView
---&gt; click on editText, opens softkeyboard
---&gt; enters string and clicks the imageView
---&gt; shows me the toast.
直到这里才好。
问题是:
我close the softkeyboard using device back button
---&gt;然后close the custom layout using ActionBar Home icon click
---&gt;现在click the searchIcon again, which reopens the custom layout, with editText filled with previously searched string
---&gt; click on the editText, the softkeyboard doesn't open up
(这是问题所在)。
请帮助,我不知道为什么它没有按预期工作。
谢谢
答案 0 :(得分:1)
我以前遇到过这个问题,这是我的解决方案
//your_menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_search"
android:orderInCategory="0"
android:icon="@drawable/btn_search"
android:title="Search"
android:actionLayout="@layout/on_search_icon_clicked"
android:showAsAction="always|collapseActionView"
/>
</menu>
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getSupportMenuInflater().inflate(R.menu.your_menu, menu);
mSearchbar = (MenuItem) menu.findItem(R.id.menu_search);
View actionview = mSearchbar.getActionView();
mEtSearchbar = ((EditText) actionview.findViewById(R.id.search_editText));
mEtSearchbar.setOnEditorActionListener(this);
mEtSearchbar.addTextChangedListener(this);
super.onCreateOptionsMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
Intent intent;
switch (item.getItemId())
{
case R.id.menu_search:
mEtSearchbar.clearFocus();
(new Handler()).postDelayed(new Runnable() {
public void run() {
mEtSearchbar.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));
mEtSearchbar.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
}
}, 100);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
mEtSearchbar~您的searchET