我有一个非常奇怪的问题。我有一个带有 fragment 的寻呼机。 fragment 之一是消息 fragment 。在此 fragment 中,我有一个包含三个选项卡 fragment 的 FragmentTabHost。在操作栏中我有一个 SearchView。当我想要搜索某些内容并开始编写查询字符串时,搜索 View 的焦点将从其中删除并设置为 TabHost 的第一个选项卡。这样我就无法编写查询字符串。仅当我靠近 MessagesFragment 时才会发生这种情况。例如:如果我足够远(1 页),则搜索工作正常。
有人可以告诉我问题出在哪里吗?
这是一张图片:
消息 fragment :
public class MessagesFragment extends Fragment {
private FragmentTabHost tabHost;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.tabHost = new FragmentTabHost(this.getActivity());
this.tabHost.setup(this.getActivity(), this.getChildFragmentManager(), container.getId());
TabSpec tabSpecForAllMessages = this.tabHost.newTabSpec("all").setIndicator("ALL");
this.tabHost.addTab(tabSpecForAllMessages, AllMessagesFragment.class, null);
TabSpec tabSpecForSentMessages = this.tabHost.newTabSpec("sent").setIndicator("SENT");
this.tabHost.addTab(tabSpecForSentMessages, SentMessagesFragment.class, null);
TabSpec tabSpecForReceivedMessages = this.tabHost.newTabSpec("received").setIndicator("RECEIVED");
this.tabHost.addTab(tabSpecForReceivedMessages, ReceivedMessagesFragment.class, null);
return this.tabHost;
}
@Override
public void onDestroyView() {
super.onDestroyView();
this.tabHost = null;
}
}
选项卡主机布局:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
操作栏菜单项:
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="@+id/menu_item_search"
android:actionViewClass="android.widget.SearchView"
android:icon="@drawable/ic_action_search"
android:showAsAction="collapseActionView|ifRoom"
android:title="@string/title_search" />
<item
android:id="@+id/menu_item_home"
android:icon="@drawable/ic_action_home"
android:showAsAction="always"
android:title="@string/title_home" />
onCreateOptionsMenu 事件处理程序:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = this.getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.menu_item_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(this
.getComponentName()));
return true;
}
最佳答案
尝试添加方法
@Override
public boolean onQueryTextChange(String text) {
if(searchView!=null)
searchView.requestFocusFromTouch();
return true;
}
searchView 必须是 init SearchView searchView;
我的 onCreateOptionsMenu 方法:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.only_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
searchView.requestFocusFromTouch();
return true;
}
});
}
这对我有用!
关于Android TabHost 和 SearchView 焦点问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20016641/