gpt4 book ai didi

Android TabHost 和 SearchView 焦点问题

转载 作者:行者123 更新时间:2023-11-29 00:25:24 24 4
gpt4 key购买 nike

我有一个非常奇怪的问题。我有一个带有 fragment 的寻呼机。其中一个 fragment 是消息 fragment 。在这个 fragment 中,我有一个带有三个选项卡 fragment 的 FragmentTabHost。在操作栏中,我有一个 SearchView。当我想要搜索某些东西并开始编写查询字符串时,搜索 View 的焦点从它移除并设置为 TabHost 的第一个选项卡。这样我就不能写查询字符串了。只有当我靠近 MessagesFragment 时才会发生这种情况。例如:如果我足够远(1 页),则搜索工作正常。

谁能告诉我问题出在哪里?

这是一张图片: Android TabHost Issue

消息 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com