自定义编辑EditText打tag标签View

package cn.baryon.here.ui;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuzhi.fine.R;

import java.util.ArrayList;

import cn.baryon.here.ui.heartview.UIUtils;

/**
 * Created by mac on 17/7/29.
 */

public class TagView extends ViewGroup {

    private ImageView imageView;
    private EditText mEditText;
    private ArrayList textViews = new ArrayList<>();
    private MarginLayoutParams mParams;
    private int childHeight = 24;
    private String tagSpace = ",";
    private String text = "";
    private String beforeText = "";
    private ArrayList textArray = new ArrayList<>();



    public TagView(Context context) {
        super(context);
    }

    public TagView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        MarginLayoutParams params = new MarginLayoutParams(UIUtils.dp2px(34), UIUtils.dp2px(34));
        imageView = new ImageView(getContext());
        imageView.setLayoutParams(params);
        imageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.tag_icon));
        addView(imageView);

        mParams = new ViewGroup.MarginLayoutParams(LayoutParams.WRAP_CONTENT,UIUtils.dp2px(childHeight));
        int margin = 5;
        mParams.leftMargin = UIUtils.dp2px(margin);  //dp转成px
        mParams.rightMargin = UIUtils.dp2px(margin);
        mParams.topMargin = UIUtils.dp2px(margin);
        mParams.bottomMargin = UIUtils.dp2px(margin);

        mEditText = new EditText(getContext());
        mEditText.setLayoutParams(mParams);
        int padding = UIUtils.dp2px(2);
        mEditText.setPadding(padding, padding, padding, padding);
        mEditText.setTextSize(13);
        mEditText.setTextColor(Color.rgb(67, 67, 67));
        mEditText.setBackgroundColor(Color.rgb(255, 255, 255));
        mEditText.setHint("打标签");
        mEditText.setFocusable(true);
        mEditText.setOnKeyListener(onKeyListener);
        mEditText.addTextChangedListener(textWatcher);
        mEditText.setVisibility(INVISIBLE);
        addView(mEditText);
        Log.i("cxblog","");
    }

    private TextView getTextView(String str){
        TextView tv = new TextView(getContext());
        tv.setLayoutParams(mParams);
        int padding = UIUtils.dp2px(4);
        tv.setPadding(padding, padding, padding, padding);
        tv.setGravity(Gravity.CENTER);
        tv.setText(str);
        tv.setTextSize(12);
        int color = Color.rgb(83, 195, 85);
        tv.setTextColor(color);
        GradientDrawable bgDrawable = new GradientDrawable();
        bgDrawable.setCornerRadius(UIUtils.dp2px(childHeight/2));
        bgDrawable.setStroke(UIUtils.dp2px(0.5), color);
        //bgDrawable.setColor(color);
        tv.setBackground(bgDrawable);
        return tv;
    }

    public void configWithText(String str){
        text = str;
        if (str.length() == 0) return;
        for (int i = 0; i" +"  "+getByteLength(text));
        for (int i = 0; i 0){
                String endString = afterText.substring(afterText.length()-1);
//                Log.i("cxblog","       endString:---"+endString);
                if (endString.equals(",") || endString.equals(",")){
                    comeToAnEnd(beforeText);
                }
            }
        }
        @Override
        public void afterTextChanged(Editable editable) {}
    };
    private void comeToAnEnd(String str){
        if (str.length() == 0) return;
        textArray.add(str);
        text = getTextWithArray(textArray);
        if (textArray.size() > 8 || getByteLength(text) > 16){
            textArray.remove(textArray.size()-1);
            text = getTextWithArray(textArray);
        }
        mEditText.setText("");
        configWithText(text);
    }

    private OnKeyListener onKeyListener = new OnKeyListener() {
        @Override
        public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
            if (keyEvent.getAction() == KeyEvent.ACTION_DOWN){
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    //Log.i("cxblog","回车键");
                    comeToAnEnd(mEditText.getText().toString());
                    // 关闭键盘
//                    Activity activity = (Activity)getContext();
//                    InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
//                    if (imm.isActive() && activity.getCurrentFocus() != null) {
//                        if (activity.getCurrentFocus().getWindowToken() != null) {
//                            imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//                        }
//                    }
                    return true;
                }
                if (keyCode == KeyEvent.KEYCODE_DEL){
                    //Log.i("cxblog","删除键");
                    if (mEditText.getText().toString().length() == 0){
                        if (textViews.size()>0){
                            textArray.remove(textArray.size()-1);
                            text = getTextWithArray(textArray);
                            Log.i("cxblog","<"+text+">" +"  "+getByteLength(text));
                            TextView lastTv = textViews.get(textViews.size()-1);
                            textViews.remove(lastTv);
                            removeView(lastTv);
                        }
                    }
                }
                return false;
            }else {
                if (keyCode == KeyEvent.KEYCODE_BACK){
                    return false;
                }else {
                    return true;
                }
            }
        }
    };

    private String getTextWithArray(ArrayList array){
        String text = "";
        for (int i = 0; i getArrayWithText(String string){
        ArrayList array = new ArrayList();
        String[] strings = string.split(tagSpace);
        for (int i = 0; i

你可能感兴趣的:(自定义编辑EditText打tag标签View)