我实际上是尝试从片段中执行搜索。我只想显示用户尝试使用SearchView小部件进行搜索的文本(使用Toast),该小部件会膨胀到片段中。根据Android文档,当用户使用SearchView小部件执行搜索时,应用程序应启动SearchableActivity。问题是我无法启动SearchableActivity。
这是我的Android Manifest:
<activity
android:name=".ClientActivity"
android:screenOrientation="nosensor"
android:label="@string/title_activity_client"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan">
<meta-data android:name="android.app.default_searchable"
android:value=".SearchableActivity" />
</activity>
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
以下是ClientActivity中调用的片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_organisation_search, container, false);
SearchView searchView = (SearchView) view.findViewById(R.id.search_view_id);
searchView.setIconified(false);
searchView.setIconifiedByDefault(false);
searchView.requestFocus();
return view;
}
它膨胀的布局:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.v7.widget.SearchView
android:id="@+id/search_view_id"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:label="@string/app_name"
android:hint="@string/search_hint"
android:background="@drawable/block">
</android.support.v7.widget.SearchView>
</ScrollView>
SearchableActivity.java:
public class SearchableActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
}
public void doMySearch(String query) {
Toast.makeText(getBaseContext(), query+"wsh", Toast.LENGTH_SHORT).show();
Log.e("The query : ",query);
}
}
这是searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_label"
android:hint="@string/search_hint" >
</searchable>
我试图让searchview小部件正常工作。如果你有任何想法我会感激。
修改 我把它的代码添加到我的片段中了:
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
答案 0 :(得分:0)
添加searchable.xml
此文件传统上名为 searchable.xml ,必须保存在res / xml / project目录中。
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_label"
android:hint="@string/search_hint" >
</searchable>