【问题标题】:How to give search option on Spinner?如何在 Spinner 上提供搜索选项?
【发布时间】:2015-08-26 16:52:32
【问题描述】:

基本上我需要建立一个带有搜索选项的微调器。真的找不到类似的东西。不过也有一些想法。喜欢它可以通过以下过程完成:

  1. 使用自动完成文本视图:这里的问题是当我单击它时我无法真正加载列表。当我开始输入时,它会显示一个列表。

  2. 在editTextView 下使用下拉列表视图。因此,当我单击 editText 时,它将充当下拉列表,当我开始键入时,项目将仅显示在列表视图中。

    这里的问题是我无法真正实现它。请帮我解决这个问题。

  3. 使用微调器 - 它的作用类似于 EditTextView,以便我可以输入。

    我需要有关如何在微调器的内部和顶部键入/放置 editTextView 的帮助。

请帮帮我。

或者如果有更好的建议也请说出来。

【问题讨论】:

    标签: android listview android-edittext spinner autocompletetextview


    【解决方案1】:

    有一个适用于 Android 的可搜索微调器库,可能对您有用。

    Searchable Spinner 是一个具有搜索功能的对话框微调器,它允许搜索在微调器中加载的项目。

    这个库只需要很少的代码更改,您只需将布局 xml 中的 Spinner 标签替换为库的标签。

    PFB链接了解更多详情SearchableSpinner

    【讨论】:

      【解决方案2】:

      我使用的是过滤,而不是自动完成。流程是,您必须填充您拥有的 Arraylist 并将其显示为普通列表。 Filtering thingy 将在您的 EditText 中实现。这是我的一些代码:

      第一个:声明您的 EditText 和一个将在其中绘制结果的 RelativeLayout:

       etClinicName = (EditText)findViewById(R.id.etClinicName);      
          btnSearch = (Button) findViewById(R.id.btnSearch);
          rlayoutResult = (RelativeLayout) findViewById(R.id.relativelayout_result);
      

      2nd:在你的 OnCreate 中,输入以下代码:

          etClinicName.addTextChangedListener(new TextWatcher()
          {
      
              @Override
              public void onTextChanged(CharSequence cs, int arg1, int arg2, int     arg3) 
              {
                  // When user changed the Text
          SearchByNameFragmentActivity.this.arrayAdapter.getFilter().filter(cs);  
              }
      
              @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
      
              }
          });
      

      这是我的 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="match_parent"
      android:background="@color/background_theme"
      android:orientation="vertical" >
      
        <RelativeLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">
      
          <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical" >
      
                  <LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="41dp"
                      android:layout_margin="20dp"
                      android:orientation="horizontal"
                      android:background="@drawable/shadow" >
      
                      <EditText
                          android:id="@+id/etClinicName"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:layout_weight="1"
                          android:hint="@string/hintSearchbranch"
                          android:inputType="textPersonName"
                          android:textSize="13sp"
                          android:background="@drawable/shadow"
                          android:paddingLeft="1dp"
                          android:gravity="center_vertical" />
      
      
      
                  </LinearLayout>
      
      
      
      
                  <RelativeLayout
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      >
      
                  <LinearLayout
                      android:layout_width="wrap_content"
                      android:layout_height="match_parent"
                      android:layout_margin="10dp">
      
                      <ListView
                          android:id="@+id/lvlClinicList"
                          android:layout_width="wrap_content"
                          android:layout_height="match_parent" />
      
      
      
                  </LinearLayout>
                  </RelativeLayout>
      
                  <TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="@string/newline" />
      
                 <RelativeLayout
                      android:id="@+id/relativelayout_result"
                      android:layout_width="wrap_content"
                      android:layout_height="10dp"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp"
                      android:layout_marginBottom="10dp" >
      
      
      
                                      <LinearLayout
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      >
      
                      <Button
                          android:id="@+id/btnSearch"
                          android:layout_width="42dp"
                          android:layout_height="wrap_content"
                          android:background="@drawable/ic_action_search_black"
                          android:padding="15dp" />
      
                      </LinearLayout>
                  </RelativeLayout>
      
      
              </LinearLayout>
      
          </RelativeLayout>
      
      
      </LinearLayout>
      

      如果您仍然感到困惑,请随时询问,希望对您有所帮助:D

      【讨论】:

      • 片段显示错误。并没有真正理解为什么有这么多列表?能否请您通过电子邮件提供源代码。我的电子邮件是:ras_shaon@yahoo.com
      • 抱歉,我忘了删除一些您可能不需要的布局。请再次检查上面的 XML 代码...您会注意到 xml 上有一个 TextView 具有 android:text="@string/newline"...请考虑在您的 String.xml 中并放入
      • 首先,确保您可以在 ArrayList 中显示您的数据/信息,如果您成功执行此操作,则过滤代码(我发布的第二个代码,检查上面的代码)不会有问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多