本文介绍了如何添加一个Android Searchbar1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有人可以一步一步为我分解如何在活动中添加搜索栏.
I was wondering if anyone could break down for me step by step how to add a searchbar in an activity .
我试图在线查找它,但是我真的不明白它是如何完成的.
I have tried to look it up online but I couldn't really understand how it's done.
推荐答案
将此依赖项添加到gradle中:
Add this dependency in your gradle :
dependencies {
compile 'com.miguelcatalan:materialsearchview:1.0.0'
}
将此添加到您的Mani活动:
Add this to your Mani Activity:
<RelativeLayout
android:id="@+id/toolbar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/theme_primary" />
<com.miguelcatalan.materialsearchview.MaterialSearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
创建菜单文件:
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_action_search"
android:orderInCategory="100"
android:title="@string/abc_search_hint"
app:showAsAction="always" />
将此添加到您的MainActivity:
Add this to your MainActivity:
MaterialSearchView searchView = (MaterialSearchView) findViewById(R.id.search_view);
searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
//Do some magic
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
//Do some magic
return false;
}
});
searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
@Override
public void onSearchViewShown() {
//Do some magic
}
@Override
public void onSearchViewClosed() {
//Do some magic
}
});
这是OnActivityResult:
Here is OnActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MaterialSearchView.REQUEST_VOICE && resultCode == RESULT_OK) {
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (matches != null && matches.size() > 0) {
String searchWrd = matches.get(0);
if (!TextUtils.isEmpty(searchWrd)) {
searchView.setQuery(searchWrd, false);
}
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
在字符串res中创建一些数组列表:
Create some array list in string res:
<string-array name="query_suggestions">
<item>Android</item>
<item>iOS</item>
<item>SCALA</item>
<item>Ruby</item>
<item>JavaScript</item>
</string-array>
将字符串res添加到搜索文本框:
Add the string res to search textbox:
searchView.setSuggestions(getResources().getStringArray(R.array.query_suggestions));
这篇关于如何添加一个Android Searchbar1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!