我有一个EditText,它的右侧有一个Drawable[V],使它看起来像一个微调器。现在,我怎样才能做到这一点呢?
我将EditText设置为可单击,然后当我单击它时,会弹出一个带有列表的对话框片段(看起来像一个微调器选项)
有可能吗?
<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>
最佳答案
您可以在XML文件中执行以下操作:在这里android:drawableRight
您可以在EditText
中设置左上和右下图标,在android中设置TextView
。
<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 Example Tutorial
或
你可以使用android弹出窗口列表视图示例。
/**
* 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 example .