首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Android中显示组合框?

如何在Android中显示组合框?
EN

Stack Overflow用户
提问于 2010-06-12 00:54:40
回答 4查看 197.6K关注 0票数 85

如何在Android中显示组合框?

EN

回答 4

Stack Overflow用户

发布于 2010-06-12 00:59:59

在android中,它被称为微调器,你可以在这里看一下教程。

Hello, Spinner

这是一个非常模糊的问题,你应该试着对你的问题进行更多的描述。

票数 80
EN

Stack Overflow用户

发布于 2011-05-19 19:32:47

没有经过测试,但你能接近的似乎是AutoCompleteTextView。您可以编写一个忽略过滤器功能的适配器。类似于:

代码语言:javascript
复制
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中:

代码语言:javascript
复制
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"),所以你不需要管理缓冲区列表。

票数 7
EN

Stack Overflow用户

发布于 2015-02-11 04:54:40

这些问题是完全正确和清晰的,因为Spinner和ComboBox (请阅读it: Spinner,您也可以在其中提供一个自定义值)是两个不同的东西。

我自己也在寻找同样的东西,但我对给定的答案并不满意。所以我创造了我自己的东西。也许有些人会发现下面的提示很有用。我没有提供完整的源代码,因为我在自己的项目中使用了一些遗留调用。无论如何,这应该是非常清楚的。

这是最后一件事的截图:

第一件事是创建一个视图,它看起来与尚未展开的微调器相同。在屏幕截图中,在屏幕的顶部(焦点模糊),你可以看到微调器和下面的自定义视图。为此,我在style="?android:attr/spinnerStyle"中使用了LinearLayout (实际上,我继承了线性布局)。LinearLayout包含带有style="?android:attr/spinnerItemStyle"的TextView。完整的XML代码片段如下:

代码语言:javascript
复制
<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文件放大的自定义视图。这是放大后的视图:

代码语言:javascript
复制
<?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。这两个选项都会设置选定值并关闭对话框。

对于列表,您希望它看起来与扩展的微调器相同,您可以为列表适配器提供适当的(微调器)样式,如下所示:

代码语言:javascript
复制
ArrayAdapter<String> adapter = 
    new ArrayAdapter<String>(
        activity,
        android.R.layout.simple_spinner_dropdown_item, 
        states
    );

或多或少,应该就是这样了。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3024656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档