gpt4 book ai didi

java - startSearch 不启动搜索 Activity

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

我在 Activity 中添加了一个 SearchView,并定义了一个搜索 Activity 来处理搜索。我添加了 OnQueryTextListener 并从 onQueryTextSubmit() 方法调用了 startSearch() 方法。当我将全局搜索设置为 true 时,它会启动谷歌搜索。当我将它设置为 false 时,没有任何反应。下面是代码:

final SearchView searchText = (SearchView) customView.findViewById(R.id.txtSearch);
searchText.setFocusable(true);
searchText.setIconifiedByDefault(false);
searchText.setQueryHint(SearchActivity.this.getResources().getString(R.string.search_text_hint));
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
// Do something
final String searchText = newText;
if (newText.equals("")) {
suggestAdapter.removeAll();
suggestAdapter.notifyDataSetChanged();
return true;
}
if (newText.length() < 4) {
return true;
}
if (!last_onTextChanged.equals(newText))
onchangeHandler.removeCallbacksAndMessages(null);
else
return true;
last_onTextChanged = newText;
onchangeHandler.postDelayed(new Runnable() {
@Override
public void run() {
getSearchSuggestions(searchText);
}
},800);
return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
// Do something
startSearch(query,true,null,false);
return true;
}
};
searchText.setOnQueryTextListener(queryTextListener);

以下定义在自定义操作栏布局 custom_search_action_bar.xml

    <ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/abc_icon"
android:layout_toEndOf="@+id/txtSearch"
android:layout_marginEnd="20dp"
android:layout_marginTop="12dp"
android:layout_marginStart="0dp"
android:background="@color/logo_green"
android:layout_centerVertical="true"
android:visibility="gone"
android:id="@+id/ic_abc_button"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/barcode_icon"
android:layout_toEndOf="@+id/txtSearch"
android:layout_marginEnd="20dp"
android:layout_marginTop="5dp"
android:layout_marginStart="0dp"
android:background="@color/logo_green"
android:layout_centerVertical="true"
android:id="@+id/ic_barcode_button"/>
</LinearLayout>
</RelativeLayout>

下面定义在res/xml/searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name">
</searchable>

最后在 AndroidManifest.xml

<activity
android:name=".view.activity.tabsearch.SearchResultsActivity"
android:label="@string/title_activity_search_results"
android:parentActivityName=".view.activity.tabsearch.SearchActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.rayat.pricewiz.view.activity.tabsearch.SearchActivity" />
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
<provider android:name="com.rayat.pricewiz.provider.SearchContentProvider"
android:authorities="com.rayat.pricewiz.provider.SearchContentProvider" />
</application>

感谢任何帮助

最佳答案

您是否已将此用于您的 MainActivity 放在 Manifest.xml 中

        <activity android:name="MainActivity">
<!-- enable the search dialog to send searches to SearchableActivity -->
<meta-data android:name="android.app.default_searchable"
android:value="SearchResultsActivity" />
</activity>

关于java - startSearch 不启动搜索 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26450290/

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