gpt4 book ai didi

Android 操作栏 SearchView 作为自动完成?

转载 作者:IT王子 更新时间:2023-10-29 00:05:28 27 4
gpt4 key购买 nike

我在操作栏中使用 SearchView。我想在搜索 View 上使用自动完成功能从数据库中获取结果。

这可能吗?还是我需要使用自定义文本框,然后为其添加自动完成功能?

最佳答案

所以我只需要为 v7 版本执行此操作,并且沮丧地发现我不能简单地使用 ArrayAdapter 设置适配器。

我不想使用普通的 AutoCompleteTextView(就像这里的顶级评论者那样),因为那样你就会错过 SearchView 的许多时髦功能,比如小搜索图标和 x 按钮。

所以我扩展了 SearchView 并得到了这个:

public class ArrayAdapterSearchView extends SearchView {

private SearchView.SearchAutoComplete mSearchAutoComplete;

public ArrayAdapterSearchView(Context context) {
super(context);
initialize();
}

public ArrayAdapterSearchView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}

public void initialize() {
mSearchAutoComplete = (SearchAutoComplete) findViewById(android.support.v7.appcompat.R.id.search_src_text);
this.setAdapter(null);
this.setOnItemClickListener(null);
}

@Override
public void setSuggestionsAdapter(CursorAdapter adapter) {
// don't let anyone touch this
}

public void setOnItemClickListener(OnItemClickListener listener) {
mSearchAutoComplete.setOnItemClickListener(listener);
}

public void setAdapter(ArrayAdapter<?> adapter) {
mSearchAutoComplete.setAdapter(adapter);
}

public void setText(String text) {
mSearchAutoComplete.setText(text);
}

}

您可以在 ActionBar 的菜单 xml 中使用它,如下所示:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >

<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_add"
android:title="TITLE"
app:actionViewClass="com.yourpackage.ArrayAdapterSearchView"
app:showAsAction="ifRoom|collapseActionView"/>

</menu>

您可能还想将点击功能添加到自动完成列表(例如,将文本设置为 EditText):

MenuItem searchItem = menu.findItem(R.id.action_search);
final ArrayAdapterSearchView searchView = (ArrayAdapterSearchView)searchItem.getActionView();
searchView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

searchView.setText(adapter.getItem(position).toString());

}
});

这里有一个与普通旧 android.widget.SearchView 类似的版本:

public class ArrayAdapterSearchView extends SearchView {

private AutoCompleteTextView mSearchAutoComplete;

public ArrayAdapterSearchView(Context context) {
super(context);
initialize();
}

public ArrayAdapterSearchView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}

public void initialize() {
mSearchAutoComplete = (AutoCompleteTextView) findViewById(getResources().getIdentifier("android:id/search_src_text", null, null));
setAutoCompleSuggestionsAdapter(null);
setOnItemClickListener(null);
}

@Override
public void setSuggestionsAdapter(CursorAdapter adapter) {
throw new UnsupportedOperationException("Please use setAutoCompleSuggestionsAdapter(ArrayAdapter<?> adapter) instead");
}

public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
mSearchAutoComplete.setOnItemClickListener(listener);
}

public void setAutoCompleSuggestionsAdapter(ArrayAdapter<?> adapter) {
mSearchAutoComplete.setAdapter(adapter);
}

public void setText(String text) {
mSearchAutoComplete.setText(text);
}

}

关于Android 操作栏 SearchView 作为自动完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15804805/

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