andriod 控件封装 可编辑下拉框

一:可编辑下拉框
android的默认Spinner只能下拉选择内容,而不能选择

二:可编辑下拉框实现方式:
1. 将下拉按钮图标添加到EditText的drawableRight属性中。
2.为按钮图标添加下拉事件,实现下拉框功能
3.将下拉框所选的值填到EditText里面
三:代码:
xml:


java代码:


/**
     * 可下拉的ExitText
     * @param et     ExitText 控件
     * @param lpw     ListPopupWindow lpw = new ListPopupWindow(getContext());
     * @param list   下拉数据
     */
    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(getContext(),
                android.R.layout.simple_list_item_1, list));
        lpw.setAnchorView(et);//设置参照控件
        lpw.setModal(true);//模态框,设置为true响应物理键
        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();
            }
        });
    }

你可能感兴趣的:(andriod 控件封装 可编辑下拉框)