gpt4 book ai didi

android - SearchView 中的下拉列表

转载 作者:行者123 更新时间:2023-11-30 01:03:03 27 4
gpt4 key购买 nike

我正在创建类似于屏幕上的 Activity 。我在工具栏中阅读了许多关于 SearchView 的教程,并在我的 Activity 中实现了它并且它有效(上面的代码)。但是如何制作这个下拉列表呢?感谢您提供想法、链接或代码。

   @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.second_activity_menu, menu);


MenuItem searchItem = menu.findItem(R.id.second_toolbar_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
// ...
}

Activity I need to create

最佳答案

在你的 gradle 中添加这个依赖:

dependencies {
compile 'com.miguelcatalan:materialsearchview:1.0.0'
}

将此添加到您的主要 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:

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:

@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);
}

在字符串资源中创建一些数组列表:

<string-array name="query_suggestions">
<item>Android</item>
<item>iOS</item>
<item>SCALA</item>
<item>Ruby</item>
<item>JavaScript</item>
</string-array>

将字符串 res 添加到搜索文本框:

searchView.setSuggestions(getResources().getStringArray(R.array.query_suggestions));

关于android - SearchView 中的下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39206368/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com