[英]Android - Implement search bar with one click
I'm trying to implement a search bar on the main Activity of my app. 我正在尝试在我的应用程序的主活动上实现搜索栏。 For that, I used the documentation from the android developer website .
为此,我使用了android开发人员网站上的文档。
I followed the instructions, and I have my search bar where I can put some query. 我按照说明进行操作,并且在搜索栏中可以放置一些查询。 The problem here is that for the few first time when I try to make a search, I have to click 2 times on a search icon.
这里的问题是,当我第一次尝试进行搜索时,必须在搜索图标上单击两次。
First, my bar looks like a default one : Search bar by default 首先,我的栏看起来像是默认栏 :默认情况下是搜索栏
I notice that the icon is not clearly white, it's like there is some transparency in it, but I've selected a full white icon. 我注意到该图标并非明显为白色,就像其中有些透明,但是我选择了一个全白色图标。
Then after a click on the icon, my bar looks like this : Search bar clicked one time 然后,单击图标后,我的栏如下所示: 搜索栏单击了一次
The icon goes to the left and I have a back button as I was already in the search field. 图标移到左侧,我有一个后退按钮,就像我在搜索字段中一样。
When I click on the search icon again, I finally have the result that I want : Search bar in edit mode 当我再次单击搜索图标时,我终于得到了想要的结果: 编辑模式下的搜索栏
When I quit the search bar and do it again, it's the same for 1 or 2 times, and then, I have the normal behavior, I go from the first image to the last just with one click. 当我退出搜索栏并再次执行此操作时,它会重复1到2次,然后我具有正常的行为,只需单击一下,我就从第一张图片转到最后一张图片。
Here's my files : 这是我的文件:
menu_app.xml menu_app.xml
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/search"
android:title="@string/search"
android:icon="@drawable/ic_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.widget.SearchView" />
</menu>
AndroidManifest.xml AndroidManifest.xml
<activity
android:name=".activities.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.SEARCH"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
searchable.xml searchable.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search"/>
OnCreateOptionsMenu in MainActivity.java MainActivity.java中的OnCreateOptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
Log.d(TAG, s);
searchView.clearFocus();
return true;
}
@Override
public boolean onQueryTextChange(String s) {
Log.d(TAG, s);
return false;
}
});
return true;
}
Thanks in advance for your help! 在此先感谢您的帮助!
我终于解决了,请查看我对解决方案的评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.