Android辅助搜索:搜索按钮不会调用可搜索的活动(其他解决方案没有帮助)
我写了这个小测试应用程序来演示问题,即当用户按下键盘上的搜索按钮时,没有启动可搜索的活动。
我一直在关注开发者指南 ,但是从我的网络搜索来看,事实certificate官方开发者指南错过了一些观点。 从我的SO搜索(没有帮助):
-
参考1:通过在清单中的元素中添加标记来解决。 我还查看了“用户词典”样本的清单(我不知道在哪里可以找到在线样本,或者我会链接到它)。 该标记位于application元素中。
-
参考2: res / xml / searchable.xml中的“android:label”和“android:hint”必须是对字符串资源的引用,而不是硬编码字符串。 我的是。
-
参考3:在活动的清单中添加一个带有“android:name =”android.app.default_searchable“”(和“android:value =”“”)的标签,搜索是将要发起。 试过这个,似乎没有用。
-
参考文献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); } }
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); } }
res / xml / searchable.xml – 可搜索配置
清单文件
布局:
activity_main.xml中
activity_test_two.xml
编辑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())); }
作为旁注:
使用default_searchable
时,我遇到了default_searchable
元标记的问题。 它似乎仅在使用完整路径(跳过风味)到搜索活动时才起作用,例如:
- 领域 – 在应用程序之间共享数据库
- 为什么android SQLite可以将double值(java 8字节)存储到float列中
- 自定义HTTP方法在Retrofit 2中不起作用
- Android模拟器不再起作用了
- SMS_RECEIVED onReceive android 2.3.5无法触发
- Android:将传感器数据从服务广播到活动
- 使用opencv的Android应用程序:如何在不覆盖的情况下保存图像?
- WARN / ActivityManager(78):启动超时已过期,放弃唤醒锁定! – >这是什么
- 返回上一个Activity,其中有一些`put extra` onClick of recyclerView Item