Sol*_*ace 6 java search android android-searchmanager android-search
我编写了这个小测试应用程序来演示问题,即当用户按下键盘上的搜索按钮时,不会启动可搜索的活动.
我一直在关注开发者指南,但是从我的网络搜索来看,事实证明官方开发者指南错过了一些观点. 从我的SO搜索(没有帮助):
参考1:通过在清单中的元素中添加标记来解决.我还查看了"用户词典"样本的清单(我不知道在哪里可以找到在线样本,或者我会链接到它).该标记位于application元素中.
参考2: res/xml/searchable.xml中的"android:label"和"android:hint"必须是对字符串资源的引用,而不是硬编码字符串.我的是.
参考3:在活动的清单中添加一个带有"android:name ="android.app.default_searchable""(以及"android:value ="<.searchable-activity-name>"")的标签.将要发起.试过这个,似乎没有用.
参考文献4: "您的可搜索活动必须做某事 - 并实际显示结果." 我的确如此,它通过ACTION_SEARCH操作接收意图,并将从intent中检索到的搜索查询字符串传递给名为"performSearch(string)"的方法,该方法在textview中显示字符串.
那么我做错了什么,我该怎么做才能解决这个问题呢?
代码: MainActivity.java - 只有一个SearchView - 用户输入查询并按下键盘上的"搜索"按钮.
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Run Code Online (Sandbox Code Playgroud)
TestTwoActivity.java
public class TestTwoActivity extends Activity {
TextView tv;
private static final String TAG = TestTwoActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_two);
/**
* The following code enables assisted search on the SearchView by calling setSearchableInfo() and passing it our SearchableInfo object.
*/
SearchView searchView = (SearchView) findViewById(R.id.searchActivity_searchView);
// SearchManager => provides access to the system search services.
// Context.getSystemService() => Return the handle to a system-level
// service by name. The class of the returned object varies by the
// requested name.
// Context.SEARCH_SERVICE => Returns a SearchManager for handling search
// Context = Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android
// system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching
// activities, broadcasting and receiving intents, etc.
// Activity.getComponentName = Returns a complete component name for this Activity
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
/**
* If the search is executed from another activity, the query is sent to this (searchable) activity in an Intent with ACTION_SEARCH action.
*/
// getIntent() Returns the intent that started this Activity
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
Log.i(TAG, "Search Query Delivered");//check
String searchQuery = intent.getStringExtra(SearchManager.QUERY);
performSearch(searchQuery);
}
}
private void performSearch(String searchQuery) {
//Just for testing purposes, I am simply printing the search query delivered to this searchable activity in a textview.
tv = (TextView) findViewById(R.id.testTwoActivity_textView);
tv.setText(searchQuery);
}
}
Run Code Online (Sandbox Code Playgroud)
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"
android:hint="@string/searchViewHint" >
</searchable>
Run Code Online (Sandbox Code Playgroud)
清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tests"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TestTwoActivity"
android:label="@string/title_activity_test_two" >
<intent-filter>
<action android:name="android.intent.action.SEARCH"/> <!-- Declares the activity to accept ACTION_SEARCH intent -->
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" /> <!-- Specifies the searchable configuration to use -->
</activity>
<!-- Points to searchable activity so the whole app can invoke search. -->
<meta-data android:name="android.app.default_searchable"
android:value=".TestTwoActivity" />
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
布局:
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tests.MainActivity" >
<android.support.v7.widget.SearchView
android:id="@+id/searchActivity_searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
activity_test_two.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<android.support.v7.widget.SearchView
android:id="@+id/searchActivity_searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/testTwoActivity_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
编辑1:我用搜索dilogue而不是搜索小部件编写了一个类似的应用程序,这很有效.
我试图在Eclipse中调试它,但调试停止,因为TestTwoActivity
(可搜索的活动)根本无法启动.
我不确定你是否忘记添加它但是你的MainActivity
未命中设置了可搜索的信息SearchView
:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SearchView searchView = (SearchView) findViewById(R.id.searchActivity_searchView);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
Run Code Online (Sandbox Code Playgroud)
作为旁注:
default_searchable
使用香精时,我对元标记有问题.它似乎仅在使用完整路径(跳过风味)到搜索活动时才起作用,例如:
<meta-data
android:name="android.app.default_searchable"
android:value="com.example.SearchActivity"/>
Run Code Online (Sandbox Code Playgroud)