一:可編輯下拉框
android的默認(rèn)Spinner只能下拉選擇內(nèi)容蒸健,而不能選擇
二:可編輯下拉框?qū)崿F(xiàn)方式:
1. 將下拉按鈕圖標(biāo)添加到EditText的drawableRight屬性中割粮。
2.為按鈕圖標(biāo)添加下拉事件,實(shí)現(xiàn)下拉框功能
3.將下拉框所選的值填到EditText里面
三:代碼:
xml:
<EditText
android:id="@+id/et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:ems="10"
android:drawableRight="@mipmap/down"
/>
java代碼:
/**
* 可下拉的ExitText
* @param et ExitText 控件
* @param lpw ListPopupWindow lpw = new ListPopupWindow(getContext());
* @param list 下拉數(shù)據(jù)
*/
private void spinnerEditText(final EditText et, final ListPopupWindow lpw, final String[] list) {
et.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_LEFT = 0;
final int DRAWABLE_TOP = 1;
final int DRAWABLE_RIGHT = 2;
final int DRAWABLE_BOTTOM = 3;
// Check if touch point is in the area of the right button
if (event.getAction() == MotionEvent.ACTION_UP) {
if (event.getX() >= (et.getWidth() - et
.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
lpw.show();
return true;
}
}
return false;
}
});
lpw.setAdapter(new ArrayAdapter<String>(getContext(),
android.R.layout.simple_list_item_1, list));
lpw.setAnchorView(et);//設(shè)置參照控件
lpw.setModal(true);//模態(tài)框,設(shè)置為true響應(yīng)物理鍵
lpw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = list[position];
et.setText(item);
lpw.dismiss();
}
});
}