我有一个Edittext,右侧有一个可绘制的[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>
答案 0 :(得分:3)
您可以在XML文件中执行以下操作:在android:drawableRight
EditText
,您可以在TextView
中设置左上方和下方的图标,在“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
用于显示像微调器一样的列表/**
* 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 AutoCompleteTextView根据保留字完成单词,因此无需编写单词的所有字符。
Android AutoCompleteTextView是一个可编辑的文本字段,它在下拉菜单中显示建议列表,用户只能从中选择一个建议或值。
Android AutoCompleteTextView是EditText类的子类。 MultiAutoCompleteTextView是AutoCompleteTextView类的子类。
Android AutoCompleteTextView Example Tutorial
或
您可以使用Android PopupWindow Listview示例。
(A1+B1+C1)/3*100
在以下链接中找到完整的实现:
答案 1 :(得分:2)
您可以使用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();
}
});
答案 2 :(得分:0)
借助@Chetan的答案,我构建了此小部件,可以帮助任何人 请使用
提供选项列表setOptions
方法
public class DropDown extends AppCompatTextView implements View.OnClickListener {
private ArrayList<String> options = new ArrayList<>();
public DropDown(Context context) {
super(context);
initView();
}
public DropDown(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public DropDown(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
this.setOnClickListener(this);
}
private PopupWindow popupWindowsort(Context context) {
// initialize a pop up window type
PopupWindow popupWindow = new PopupWindow(context);
popupWindow.setWidth(this.getWidth());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line,
options);
// the drop down list is a list view
ListView listViewSort = new ListView(context);
// set our adapter and pass our pop up window contents
listViewSort.setAdapter(adapter);
// set on item selected
listViewSort.setOnItemClickListener((parent, view, position, id) -> {
this.setText(options.get(position));
popupWindow.dismiss();
});
// some other visual settings for popup window
popupWindow.setFocusable(true);
// popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.white));
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
// set the listview as popup content
popupWindow.setContentView(listViewSort);
return popupWindow;
}
@Override
public void onClick(View v) {
if (v == this) {
PopupWindow window = popupWindowsort(v.getContext());
window.showAsDropDown(v, 0, 0);
}
}
public void setOptions(ArrayList<String> options) {
this.options = options;
}
}
在您的布局文件中
<com.yourdomian.app.DropDown
style="@style/formDropDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/ship_to_address" />
在样式文件中
<style name="formDropDown">
<item name="android:paddingRight">20dp</item>
<item name="android:paddingLeft">24dp</item>
<item name="android:paddingTop">20dp</item>
<item name="android:paddingBottom">20dp</item>
<item name="android:textSize">13sp</item>
<item name="android:background">@drawable/edit_text_background_dark_round</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:elevation">5dp</item>
<item name="android:drawableRight">@drawable/ic_down_arrow</item>
<item name="android:gravity">center_vertical</item>
</style>
在您的Java文件中
ArrayList<String> options = new ArrayList<>();
options.add("Option 1");
options.add("Option 2");
options.add("Option 3");
options.add("Option 4");
((DropDown)findViewById(R.id.dropdown)).setOptions(options);
答案 3 :(得分:0)
public class DropDownEditText extends AutoCompleteTextView {
private float x;
public DropDownEditText(Context context, AttributeSet attrs) {
super(context, attrs);
setCompoundDrawablesWithIntrinsicBounds(null, null, context.getDrawable(R.drawable.ic_spinner_caret), null);
setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, new String[]{"one", "two", "three", "four"}));
}
@Override
public boolean performClick() {
if (x < (getRight() - getCompoundDrawables()[2].getBounds().width()))
return super.performClick();
showDropDown();
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
x = event.getX();
}
return super.onTouchEvent(event);
}
}