我是java和android开发的新手; 我一直在开发一个应用程序,我需要一个带有操作栏的操作栏SearchView.我看过谷歌的例子,但我无法让它工作.我一定做错了,我即将放弃应用程序开发:DI已经搜索但我没有找到任何有用的东西.也许你们可以帮助我:)
问题:我得到的搜索视图应该打开,但在此之后什么也没发生,我注意到我的searchManager.getSearchableInfo(getComponentName())返回null.另外,我给出的提示没有显示在我的搜索框中,这让我相信应用程序可能无法找到我的搜索文件?够了:)
MainActivity.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_hint"
android:label="@string/app_label">
</searchable>
Run Code Online (Sandbox Code Playgroud)
Androidmanifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.searchapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" …Run Code Online (Sandbox Code Playgroud) 我按照http://developer.android.com/guide/topics/search/search-dialog.html上描述的步骤在我的记事本应用程序中实现搜索功能.
我的问题是,当我完成搜索时,新的活动会打开捕获我的搜索查询.但我真正想要的是,查询返回到当前活动而不是开始新活动.
这可能吗?
更新:
AndroidManifest.xml中
<activity android:name="MyNotepad"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEARCH"></action>
</intent-filter>
<meta-data android:resource="@xml/searchable" android:name="android.app.searchable"></meta-data>
</activity><activity android:name="Preferences" android:label="Preferences" >
</activity>
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/search_hint">
</searchable>
JAVA代码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_pad);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Toast.makeText(getApplicationContext(), query, Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.menuItemSearch:
startSearch("", false, null, false);
break;
}
return true; …Run Code Online (Sandbox Code Playgroud) 我的用例如下:在活动A中,我有一个带有可折叠SearchView的操作栏.当用户提出查询并按下"搜索"按钮时,我想显示活动B的结果.我没有这样做,这是我的代码:
searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/action_search_hint"
android:label="@string/app_name"/>
Run Code Online (Sandbox Code Playgroud)
活动A:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
Run Code Online (Sandbox Code Playgroud)
活动A和B的AndroidManifest:
<activity
android:name=".ActivityA">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".ActivityB">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
Run Code Online (Sandbox Code Playgroud)
Howerver,无论我尝试什么,A中的getSearchableInfo始终返回null.
我感觉我不理解某些东西.我可以使getSearchableInfo()返回非null的唯一方法是将SEARCH意图过滤器和可搜索的元数据放入清单中的活动A. 但是,当我按下"搜索"按钮时,活动A的另一个实例再次在当前的实例之上启动,这绝对不是我想要的.我想我可以将singleTop添加到A并在onNewIntent中处理SEARCH动作并启动B,但我通常不希望A成为singleTop,我想要默认值.或者我呢?
我尝试在清单中将其添加到A中(如其他一些线程所示):
<meta-data
android:name="android.app.default_searchable"
android:value=".ActivityB" >
</meta-data>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
编辑:我用A替换了A中的'getComponentName()'行:
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, ActivityB.class)));
Run Code Online (Sandbox Code Playgroud)
它似乎按我想要的方式工作!我在这里做错了什么,或者这是正确的方法吗?我有点不确定,因为它在任何地方都没有提到!