为Android Spinner创建文本过滤器(类似于快速搜索)

15
我正在开发一个Android应用程序。我在这里有我的活动和一些小部件,包括Spinner。我希望使用快速搜索按钮来搜索Spinner对象。想法是用户点击Spinner对象,然后看到列表(适配器)。如果他点击快速搜索按钮,他应该提供一个文本字段来输入一个字母,然后Spinner列表跳到它找到的第一个单词与提供的字母相匹配。就像html和select标签一样工作。
我尝试过谷歌(当然还有SO),但似乎没有人对这样的解决方案感兴趣或者这是一个严密保密的秘密。 :)
你有关于这个主题的一些指针吗?

我曾经有一种解决方案,但现在我在另一个地方工作,无法查看代码库。 - Adam Arold
3个回答

8

嗯,该应用程序使用了一个特殊的Spinner对象(扩展自Spinner),其中包含键值对。此外,它必须是下拉菜单而不是编辑文本字段。我认为这样做不会奏效。 - Adam Arold
5
那么这怎么成为被接受的答案呢?尽管没有输入值,但旋转选择器仍然允许进行选择。 - Aritz
正如您所看到的,这个问题已经三年了。我已经有一年没有使用Android了,所以它已经不再相关了。 - Adam Arold

6

您可以自己实现它。

使用按钮代替旋转器,设计一个带有 EditText 用于查询输入和 ListView 用于显示内容的对话框。然后根据用户在 EditText 中输入的内容过滤 ListView 的内容。

filter(list, text);
adapter.notifyDataSetChanged(); 

这是一个非常好的解决方案!不需要第三方库。 - Gabriel

6

我知道这个问题很久远,但今天我也需要这个函数。因为我找不到任何东西,所以我自己为那些微调器做了一个适配器。

适配器:

public class Searchspinner extends ArrayAdapter<String> {
    private LayoutInflater inflater;
    private boolean dropdown = false;
    private OnClickListener onsearch;
    private ActionBar ab;
    private final ArrayList<String> result = new ArrayList<String>();
    private InputMethodManager keyboard;
    private boolean searching = false;
    public Searchspinner(Context context, int resource,
            ArrayList<String> objects, LayoutInflater l, ActionBar a,
            InputMethodManager imm, String spinnerid) {
        super(context, resource, objects);
        inflater = l;
        ab = a;
        keyboard = imm; 
        createSearch();
        // TODO Auto-generated constructor stub
    }
    @Override
    public View getDropDownView(int position, View cnvtView, ViewGroup prnt{
        dropdown = true;
        return getCustomView(position, cnvtView, prnt);
    }
    @Override
    public View getView(int pos, View cnvtView, ViewGroup prnt) {
        dropdown = false;
        return getCustomView(pos, cnvtView, prnt);
    }
    public View getCustomView(int position, View convertView, ViewGroup     parent) {
        if (!dropdown) {
            View mySpinner = inflater.inflate(
                    R.layout.spinner_ressource_search, parent, false);
            TextView main_text = (TextView) mySpinner
                    .findViewById(R.id.tv_spinner_first);
            main_text.setText(getItem(position));
            ImageButton search = (ImageButton) mySpinner
                    .findViewById(R.id.searchbutton);
            if (!searching) {
                search.setImageResource(R.drawable.search);
            } else {
                search.setImageResource(R.drawable.search_check);
            }
            search.setOnClickListener(onsearch);
            return mySpinner;
        }
        View mySpinner = inflater.inflate(R.layout.auftragtextview, parent,
                false);
        TextView sub_text = (TextView)  mySpinner.findViewById(R.id.TextView01);
        sub_text.setText(getItem(position));
        return mySpinner;
    }
    private void createSearch() {
        onsearch = new OnClickListener() {
            @Override
            public void onClick(View v) {       
                // TODO Auto-generated method stub
                if (searching) {
                    searching = false;
                    ab.setCustomView(R.layout.actionbar_layout);
                    ab.getCustomView().setTag("0");
                    keyboard.toggleSoftInput(0,
                            InputMethodManager.HIDE_IMPLICIT_ONLY);
                    for (int i = 0; i < result.size(); i++) {
                        add(result.get(i));
                        result.remove(i);
                        i--;
                    }
                    ((ImageButton)  v).setImageResource(R.drawable.search);
                    return;
                }
                ((ImageButton)  v).setImageResouce(R.drawable.search_check);
                searching = true;
                ab.setCustomView(R.layout.searchable);
                final EditText et = (EditText) ab.getCustomView()
                        .findViewById(R.id.editText1);
                et.setActivated(true);
                    keyboard.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
                        0);
                et.requestFocus();
                et.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void onTextChanged(CharSequence s, int start,
                            int before, int count) {
                        for (int i = 0; i < getCount(); i++) {
                            if (!getItem(i).contains(s)) {
                                result.add(getItem(i));
                                remove(getItem(i));
                                i--;
                            }
                        }
                        for (int i = 0; i < result.size(); i++) {
                            if (result.get(i).toLowerCase()
                                        .contains(s.toString().toLowerCase())) {
                                add(result.get(i));
                                result.remove(i);
                                i--;
                            }
                        }
                    }
                    @Override
                    public void beforeTextChanged(CharSequence s,
                            int start, int count, int after) {
                        // TODO Auto-generated method stub
                    }
                    @Override
                    public void afterTextChanged(Editable s) {
                        // TODO Auto-generated method stub
                    }
                });
            }
        };
    }
}

spinner_ressource_search.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/tv_spinner_first"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="TextView" />

<ImageButton
    android:id="@+id/searchbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@android:color/transparent"
    android:src="@drawable/search" />

</RelativeLayout>

以及auftragtextview.xml文件:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TextView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="@+d/TextView01"
    android:textSize="16sp" >
</TextView>

以下是创建适配器的方法:

Searchspinner auftragSpinnerAdapter = new Searchspinner(
    this.getApplicationContext(),
    R.layout.auftragtextview,
    list_auftragSpinner,getLayoutInflater(),
    getActionBar(),
    (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)
);  

我希望这能对任何人有所帮助,而且我没有漏掉已经内置的做法:D
问候
编辑:
searcheble.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/actionbar_background_beosys" >
<EditText
    android:id="@+id/editText1"
    android:hint="@string/suche"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:layout_marginLeft="15dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:layout_marginRight="15dp"
    android:singleLine="true"
  /> 

EditText1 就是当你搜索时在视图中显示的简单的 EditText。

ActionbarLayout 是普通的 ActionbarView。


1
searchableactionbar_layouteditText1 项目是什么? - ShahiM
4
actionbar_layout 在答案中仍未定义。 - Juliatzin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接