如何在Android中显示组合框?
发布于 2010-06-12 00:59:59
发布于 2011-05-19 19:32:47
没有经过测试,但你能接近的似乎是AutoCompleteTextView。您可以编写一个忽略过滤器功能的适配器。类似于:
class UnconditionalArrayAdapter<T> extends ArrayAdapter<T> {
final List<T> items;
public UnconditionalArrayAdapter(Context context, int textViewResourceId, List<T> items) {
super(context, textViewResourceId, items);
this.items = items;
}
public Filter getFilter() {
return new NullFilter();
}
class NullFilter extends Filter {
protected Filter.FilterResults performFiltering(CharSequence constraint) {
final FilterResults results = new FilterResults();
results.values = items;
return results;
}
protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
items.clear(); // `items` must be final, thus we need to copy the elements by hand.
for (Object item : (List) results.values) {
items.add((String) item);
}
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
..。然后在你的onCreate中:
String[] COUNTRIES = new String[] {"Belgium", "France", "Italy", "Germany"};
List<String> contriesList = Arrays.asList(COUNTRIES());
ArrayAdapter<String> adapter = new UnconditionalArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, contriesList);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
代码没有经过测试,可能有一些我没有考虑过的过滤方法的功能,但现在您已经了解了用AutoCompleteTextView模拟ComboBox的基本原则。
编辑固定的NullFilter实现。我们需要访问这些项,因此UnconditionalArrayAdapter
的构造函数需要引用一个列表(一种缓冲区)。你也可以先使用adapter = new UnconditionalArrayAdapter<String>(..., new ArrayList<String>);
,然后再使用adapter.add("Luxemburg")
,所以你不需要管理缓冲区列表。
发布于 2015-02-11 04:54:40
这些问题是完全正确和清晰的,因为Spinner和ComboBox (请阅读it: Spinner,您也可以在其中提供一个自定义值)是两个不同的东西。
我自己也在寻找同样的东西,但我对给定的答案并不满意。所以我创造了我自己的东西。也许有些人会发现下面的提示很有用。我没有提供完整的源代码,因为我在自己的项目中使用了一些遗留调用。无论如何,这应该是非常清楚的。
这是最后一件事的截图:
第一件事是创建一个视图,它看起来与尚未展开的微调器相同。在屏幕截图中,在屏幕的顶部(焦点模糊),你可以看到微调器和下面的自定义视图。为此,我在style="?android:attr/spinnerStyle"
中使用了LinearLayout (实际上,我继承了线性布局)。LinearLayout包含带有style="?android:attr/spinnerItemStyle"
的TextView。完整的XML代码片段如下:
<com.example.comboboxtest.ComboBox
style="?android:attr/spinnerStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/textView"
style="?android:attr/spinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:text="January"
android:textAlignment="inherit"
/>
</com.example.comboboxtest.ComboBox>
正如我前面提到的,ComboBox继承自LinearLayout。它还实现了OnClickListener,它创建了一个对话框,其中包含一个从XML文件放大的自定义视图。这是放大后的视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="Enter custom value ..." >
<requestFocus />
</EditText>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="OK"
/>
</LinearLayout>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
您还需要实现另外两个侦听器:用于列表的onItemClick和用于按钮的onClick。这两个选项都会设置选定值并关闭对话框。
对于列表,您希望它看起来与扩展的微调器相同,您可以为列表适配器提供适当的(微调器)样式,如下所示:
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(
activity,
android.R.layout.simple_spinner_dropdown_item,
states
);
或多或少,应该就是这样了。
https://stackoverflow.com/questions/3024656
复制相似问题