编辑文本中的微调器

新手上路,请多包涵

我有一个 Edittext,在它的右侧有一个可绘制的 [v],使它看起来像一个微调器。现在,我怎样才能做到这一点?我会将 edittext 设置为可点击,然后当我点击它时,将弹出一个带有列表的 dialogfragment(看起来像一个微调器选项)

可能吗?

    <android.support.design.widget.TextInputLayout
    android:id="@+id/tilAppCategory"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edtAppCategory"
        android:hint="Category"
        android:fontFamily="sans-serif-light"
        android:textColorHint="@color/textColorHint"
        android:maxLines="1"
        android:gravity="center|start"
        android:inputType="textNoSuggestions"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="14sp"
        android:drawableEnd="@drawable/icon_spinner_down"
        android:focusableInTouchMode="true"
        android:clickable="true"
        />

</android.support.design.widget.TextInputLayout>

原文由 Paul John Pulumbarit 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 297
2 个回答

您可以在 XML 文件中执行以下操作:在这里 android:drawableRight 您可以在 EditTextTextView 中设置左右顶部和底部图标 andorid—

 <EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:drawableRight="@drawable/ic_menu_share"/>

要显示像微调器这样的列表,请使用 AutoCompleteTextView

Android AutoCompleteTextView 是根据保留字补全单词,所以不需要写单词的所有字符。

Android AutoCompleteTextView 是一个可编辑的文本字段,它在下拉菜单中显示建议列表,用户只能从中选择一个建议或值。

Android AutoCompleteTextView 是 EditText 类的子类。 MultiAutoCompleteTextView 是 AutoCompleteTextView 类的子类。

Android AutoCompleteTextView 示例教程

要么

您可以使用 Android PopupWindow Listview 示例。

 /**
  * handle header listview onclick event
  */
 private OnClickListener showPopupWindow() {
  return new OnClickListener() {

   @Override
   public void onClick(View v) {
    PopupWindow popUp = popupWindowsort();
    popUp.showAsDropDown(v, 0, 0); // show popup like dropdown list
   }
  };
 }

 /**
  * show popup window method reuturn PopupWindow
  */
 private PopupWindow popupWindowsort() {

  // initialize a pop up window type
  popupWindow = new PopupWindow(this);

  ArrayList<String> sortList = new ArrayList<String>();
  sortList.add("A to Z");
  sortList.add("Z to A");
  sortList.add("Low to high price");

  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,
    sortList);
  // the drop down list is a list view
  ListView listViewSort = new ListView(this);

  // set our adapter and pass our pop up window contents
  listViewSort.setAdapter(adapter);

  // set on item selected
  listViewSort.setOnItemClickListener(onItemClickListener());

  // some other visual settings for popup window
  popupWindow.setFocusable(true);
  popupWindow.setWidth(250);
  // popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.white));
  popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

  // set the listview as popup content
  popupWindow.setContentView(listViewSort);

  return popupWindow;
 }

在以下链接中找到完整的实现:

Android PopupWindow Listview 示例。

原文由 Chetan Joshi 发布,翻译遵循 CC BY-SA 3.0 许可协议

您可以使用 autocompletetextview 来实现这一点

<AutoCompleteTextView
                        android:id="@+id/acType"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:maxLines="1"
                        android:paddingBottom="@dimen/lef_margin"
                        android:paddingTop="@dimen/lef_margin"
                        android:singleLine="true"
                        android:textSize="@dimen/header_text_large"/>

ArrayAdapter arrayAdapter= new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, dataList);
    acType.setAdapter(arrayAdapter);
    acType.setInputType(0);

acType.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus)
                acType.showDropDown();
        }
    });

原文由 Chirag.T 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题