2011-04-27 70 views
12

我正在開發一個Android應用程序。我在這裏有一些小工具,包括微調。我希望使用快速搜索按鈕可以搜索該Spinner對象。這個想法是,用戶點擊Spinner對象,他看到列表(適配器)。如果他點擊快速搜索按鈕,他應該提供一個文本字段來輸入一個字母,然後微調列表跳轉到它提供的字母找到的第一個單詞。就像它與html和選擇標籤一樣。爲Android中的微調器創建文本過濾器(如快速搜索)

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

  • 無人問津像這樣
  • 的解決方案是一個諱莫如深。 :)

你對這個主題有一些指導嗎?

+1

你有什麼解決辦法嗎? – 2012-07-04 18:32:50

+0

那麼我有一些解決方案,但我現在正在其他地方工作,我無法看到存儲庫。 – 2012-07-04 22:02:12

回答

8

看起來你在談論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

5

你可以通過你的自我實現它給出了類似的功能。

使用按鈕代替微調器,設計一個對話框,其中EditText用於查詢輸入,ListView用於要顯示的內容。 然後根據在EditText中輸入的用戶來篩選ListView的內容。

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

漂亮的解決方案夥計 – 2012-07-05 11:29:34

+0

這真是一個不錯的解決方案!不需要第三個庫 – Gabriel 2017-08-24 18:54:00

6

我知道這個問題是舊的,但今天我也需要此功能,因爲我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