为Android中的微调器创建文本过滤器(如快速搜索)

为Android中的微调器创建文本过滤器(如快速搜索)

问题描述:

我正在开发一个Android应用程序。我在这里有一些小工具,包括微调。我希望使用快速搜索按钮可以搜索该Spinner对象。这个想法是,用户点击Spinner对象,他看到列表(适配器)。如果他点击快速搜索按钮,他应该提供一个文本字段来输入一个字母,然后微调列表跳转到它提供的字母找到的第一个单词。就像它与html和选择标签一样。为Android中的微调器创建文本过滤器(如快速搜索)

我想谷歌(和SO的课程),但似乎

  • 无人问津像这样
  • 的解决方案是一个讳莫如深。 :)

你对这个主题有一些指导吗?

+1

你有什么解决办法吗? – 2012-07-04 18:32:50

+0

那么我有一些解决方案,但我现在正在其他地方工作,我无法看到存储库。 – 2012-07-04 22:02:12

看起来你在谈论AutoCompleteTextView

+0

那么,应用程序正在使用一个特殊的Spinner对象(从Spinner扩展),它具有键值对。另外,它必须是下拉菜单,而不是编辑文本字段。我不认为它会这样工作。 – 2011-04-27 09:24:42

+1

@AdamArold,这怎么可能被接受呢?即使没有引入输入值,纺纱厂也可以选择。 – 2014-11-03 07:39:43

+0

正如你可以看到这个问题是3岁。我不使用Android 1年,所以它不再相关。 – 2014-11-03 14:27:53

你可以通过你的自我实现它给出了类似的功能。

使用按钮代替微调器,设计一个对话框,其中EditText用于查询输入,ListView用于要显示的内容。 然后根据在EditText中输入的用户来筛选ListView的内容。

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

漂亮的解决方案伙计 – 2012-07-05 11:29:34

+0

这真是一个不错的解决方案!不需要第三个库 – Gabriel 2017-08-24 18:54:00

我知道这个问题是旧的,但今天我也需要此功能,因为我wasn't能找到任何东西,我自己做了这些微调

适配器一个适配器:

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

我希望这会帮助别人,那我haven't错过了已经建立的方式来做到这一点: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只是在ActionBar中get's当您搜索查看一个简单的EDITTEXT。

ActionbarLayout是普通的ActionbarView。

+0

什么是“可搜索”,“actionbar_layout”和“editText1”项目? – ShahiM 2015-08-05 09:30:36

+1

编辑答案 – glm9637 2015-08-07 06:57:54

+3

actionbar_layout仍未在答案中定义 – 2015-10-21 22:40:39