我实际上是尝试从片段中执行搜索。我只想显示用户尝试使用SearchView小部件进行搜索的文本(使用Toast),该小部件会膨胀到片段中。根据Android文档,当用户使用SearchView小部件执行搜索时,应用程序应启动SearchableActivity。问题是我无法启动SearchableActivity。
这是我的Android Manifest(与Berhan zikarge编辑回复):
<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) {
// Inflate the layout for this fragment
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);
}
}
//Here no log is displayed in logcat, and no Toast is shown on the app
public void doMySearch(String query) {
Toast.makeText(getBaseContext(), query+"wsh", Toast.LENGTH_SHORT).show();
Log.e("The query : ",query);
}
}
我已经纠结了这个问题超过2个小时,如果有人有解决方案,我会很感激。
修改 我把它的代码添加到我的片段中了:
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
答案 0 :(得分:0)
我认为您需要将元数据标记放在发送搜索数据的活动上,而不是接收数据的活动。 ClientActivity应具有元数据标记:
<!-- this activity enables the search dialog to initiate searches
in the SearchableActivity -->
<activity android:name=".OtherActivity" ... >
<!-- enable the search dialog to send searches to SearchableActivity -->
<meta-data android:name="android.app.default_searchable"
android:value=".SearchableActivity" />
</activity>
...
https://developer.android.com/guide/topics/search/search-dialog.html
答案 1 :(得分:0)
问题是客户端活动的元数据不在activity标记之内。将以下代码复制并粘贴到清单
中 <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>