自定义控件>带下拉框的文本框 (DropEditView)

package xxx.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.inputmethodservice.Keyboard;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ScrollView;

import xxx.R;
import xxx.keyboard.KeyBoardUtils;
import xxx.string.DensityUtils;

/**
 * Created by xlzhen on 6/25 0025.
 * 仿spinner控件,可实现edit_text旁的图片变化和下拉列表item的深度定制
 */
public class DropEditView extends LinearLayout {
    private EditText editText;
    private TextColorButton colorButton;
    private Context context;
    private ScrollView scrollView;
    private ImageView imageView;
    private LinearLayout linearLayout_Top, linearLayout_Bottom;
    private int position;

    private PopupWindow popupWindow;

    private static int edit_width = 40;

    private int offset_left=5;
    private int popup_height = ViewGroup.LayoutParams.WRAP_CONTENT;

    /*
    * 实例化dropeditview(新增edit_text,scroview)
    *
    */

    public DropEditView(Context context,int margin) {
        super(context);
        setOrientation(VERTICAL);

        this.context = context;

        position = 0;

        linearLayout_Top = new LinearLayout(context);
        linearLayout_Top.setOrientation(HORIZONTAL);
        linearLayout_Top.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , ViewGroup.LayoutParams.WRAP_CONTENT));

        editText = new EditText(context);
        editText.setFocusable(false);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtils.dp2px(edit_width, context));
        layoutParams.setMargins(DensityUtils.dp2px(margin, context), DensityUtils.dp2px(margin, context)
                , DensityUtils.dp2px(margin, context), DensityUtils.dp2px(margin, context));
        editText.setLayoutParams(layoutParams);
        editText.setGravity(Gravity.CENTER);
        editText.setTextSize(15f);
        editText.setHintTextColor(getResources().getColor(R.color.shangjia_biaoqian_grid_color));
        editText.setBackgroundResource(R.drawable.edit_text_layout);
        editText.setClickable(true);
        editText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setShowPopup(popup_height);
            }
        });


        linearLayout_Top.addView(editText);


        scrollView = new ScrollView(context);
        scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        linearLayout_Bottom = new LinearLayout(context);
        linearLayout_Bottom.setOrientation(VERTICAL);
        linearLayout_Bottom.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , ViewGroup.LayoutParams.MATCH_PARENT));


        scrollView.addView(linearLayout_Bottom);

        addView(linearLayout_Top);

    }

    public DropEditView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(VERTICAL);

        this.context = context;

        position = 0;

        linearLayout_Top = new LinearLayout(context);
        linearLayout_Top.setOrientation(HORIZONTAL);
        linearLayout_Top.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , ViewGroup.LayoutParams.WRAP_CONTENT));

        editText = new EditText(context);
        editText.setFocusable(false);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtils.dp2px(edit_width, context), 1.0f);
        layoutParams.setMargins(DensityUtils.dp2px(5, context), DensityUtils.dp2px(10, context)
                , DensityUtils.dp2px(10, context), DensityUtils.dp2px(10, context));
        editText.setLayoutParams(layoutParams);
        editText.setGravity(Gravity.CENTER);
        editText.setTextSize(15f);
        editText.setHintTextColor(getResources().getColor(R.color.shangjia_biaoqian_grid_color));
        editText.setBackgroundResource(R.drawable.edit_text_layout);
        editText.setClickable(true);
        editText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setShowPopup(popup_height);
            }
        });


        linearLayout_Top.addView(editText);


        scrollView = new ScrollView(context);
        scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        linearLayout_Bottom = new LinearLayout(context);
        linearLayout_Bottom.setOrientation(VERTICAL);
        linearLayout_Bottom.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , ViewGroup.LayoutParams.MATCH_PARENT));


        scrollView.addView(linearLayout_Bottom);

        addView(linearLayout_Top);

    }

    public void setPopup_height(int popup_height) {
        this.popup_height = DensityUtils.dp2px(popup_height, context);
    }

    private void setShowPopup(int popup_height) {

        if (popupWindow == null) {
            popupWindow = new PopupWindow(scrollView, editText.getWidth() - DensityUtils.dp2px(offset_left-10,context), popup_height);
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setOutsideTouchable(true);
            popupWindow.setFocusable(true);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    if (imageView != null) {
                        imageView.setBackgroundResource(R.mipmap.me_btn_cbb);
                    }
                }
            });
        }
        if (imageView != null) {
            imageView.setBackgroundResource(R.mipmap.me_btn_cbb_pre);
        }
        KeyBoardUtils.closeKeybord(editText, editText.getContext());
        popupWindow.showAsDropDown(editText);

    }

    /*
        * 如果需要edit_text旁边加图片,则set该项
        *
        * */
    public void setRightImageView(int offset_left,int margin) {
        this.offset_left=Math.abs(offset_left);
        if (imageView == null) {
            if (scrollView != null) {
                LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT
                        , LayoutParams.MATCH_PARENT);
                layoutParams.setMargins(0, 0, DensityUtils.dp2px(edit_width, context), 0);
                scrollView.setBackgroundColor(Color.WHITE);
                scrollView.setLayoutParams(layoutParams);
            }
            imageView = new ImageView(context);

            LayoutParams layoutParams = new LayoutParams(DensityUtils.dp2px(edit_width, context)
                    , DensityUtils.dp2px(edit_width, context));
            layoutParams.setMargins(DensityUtils.dp2px(offset_left, context), DensityUtils.dp2px(margin, context)
                    , DensityUtils.dp2px(margin, context), DensityUtils.dp2px(margin, context));
            imageView.setLayoutParams(layoutParams);
            imageView.setClickable(true);
            imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    setShowPopup(popup_height);
                }
            });

            imageView.setBackgroundResource(R.mipmap.me_btn_cbb);
            if (linearLayout_Top != null) {
                linearLayout_Top.addView(imageView);
            }
        }
    }

    /*
    * 添加textcolorbutton到scroview
    * */
    public void addColorButton(final String text, int text_select_color, int text_unselect_color, final int position) {
        colorButton = new TextColorButton(context);
        colorButton.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , ViewGroup.LayoutParams.WRAP_CONTENT));
        colorButton.setText(text);
        colorButton.setTextColorChanged(text_select_color, text_unselect_color);

        colorButton.setButtonLayout();
        colorButton.setTextLayout();

        colorButton.setBackgroundResource(R.drawable.dropdown_layout);
        colorButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();

                if (imageView != null) {
                    imageView.setBackgroundResource(R.mipmap.me_btn_cbb);
                }

                DropEditView.this.setEditText(text);
                DropEditView.this.position = position;
            }
        });
        linearLayout_Bottom.addView(colorButton);
    }

    /*
    * 写入edittext
    * */
    public void setEditText(String text) {
        if (editText != null) {
            editText.setText(text);
        }
    }

    /*
    * 更改文本后更新
    * */
    public void setTextChanged(TextWatcher textChanged) {
        editText.addTextChangedListener(textChanged);
    }

    /*
    * 读取edit_text,返回的是下拉列表的position
    * */
    public int getEditPosition() {

        return position+1;
    }

    /*
        * 读取edit_text,返回的是String
        * */
    public String getEditText() {

        return editText.getText().toString();
    }

    /*
    * 设置edit不可写入
    * */
    public void setEditTextable() {
        editText.setFocusable(false);
        editText.setFocusableInTouchMode(false);
    }

    public void clearButton() {
        linearLayout_Bottom.removeAllViews();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值