From 9c77326126518b5c55acca226711e308e3a8335f Mon Sep 17 00:00:00 2001 From: classichu Date: Fri, 17 Mar 2017 14:40:36 +0800 Subject: [PATCH] update --- .../lineseditview/LinesEditView.java | 133 +++++++++++------- .../res/layout/layout_lines_edit_view.xml | 20 ++- library/src/main/res/values/attrs.xml | 5 +- 3 files changed, 99 insertions(+), 59 deletions(-) diff --git a/library/src/main/java/com/classichu/lineseditview/LinesEditView.java b/library/src/main/java/com/classichu/lineseditview/LinesEditView.java index ad007ea..bff96ab 100644 --- a/library/src/main/java/com/classichu/lineseditview/LinesEditView.java +++ b/library/src/main/java/com/classichu/lineseditview/LinesEditView.java @@ -2,9 +2,11 @@ import android.content.Context; import android.content.res.TypedArray; +import android.graphics.Color; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; +import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; @@ -15,42 +17,48 @@ * ignoreCnOrEn 为false的时候 * 1个中文算1个 * 2个英文算1个 - * + *

* 另外:如:只有一个英文时也算1个 - + *

* Created by louisgeek on 2016/9/19. */ -public class LinesEditView extends LinearLayout{ - private Context mContext; - private EditText id_et_input; - private TextView id_tv_input; +public class LinesEditView extends LinearLayout { + private Context mContext; + private EditText id_et_input; + private TextView id_tv_input; private int MAX_COUNT; private String hintText; + private int hintTextColor; private boolean ignoreCnOrEn; private String contentText; - private float contentHeight; + private int contentTextSize; + private int contentTextColor; + private float contentViewHeight; - LinearLayout id_ll_multi; public LinesEditView(Context context) { - this(context,null); + this(context, null); } public LinesEditView(Context context, AttributeSet attrs) { - this(context, attrs,0); + this(context, attrs, 0); } public LinesEditView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); - mContext=context; + mContext = context; TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LinesEditView); - MAX_COUNT=typedArray.getInteger(R.styleable.LinesEditView_classic_maxCount,240); - ignoreCnOrEn=typedArray.getBoolean(R.styleable.LinesEditView_classic_ignoreCnOrEn,true); - hintText=typedArray.getString(R.styleable.LinesEditView_classic_hintText); - contentText=typedArray.getString(R.styleable.LinesEditView_classic_contentText); - contentHeight=typedArray.getDimensionPixelSize(R.styleable.LinesEditView_classic_contentHeight,dp2px(getContext(),140)); + MAX_COUNT = typedArray.getInteger(R.styleable.LinesEditView_classic_maxCount, 240); + ignoreCnOrEn = typedArray.getBoolean(R.styleable.LinesEditView_classic_ignoreCnOrEn, true); + hintText = typedArray.getString(R.styleable.LinesEditView_classic_hintText); + hintTextColor = typedArray.getColor(R.styleable.LinesEditView_classic_hintTextColor, Color.parseColor("#42000000")); + contentText = typedArray.getString(R.styleable.LinesEditView_classic_contentText); + contentTextColor = typedArray.getColor(R.styleable.LinesEditView_classic_contentTextColor, Color.parseColor("#8A000000")); + contentTextSize = typedArray.getDimensionPixelSize(R.styleable.LinesEditView_classic_contentTextSize, dp2px(context, 14)); + contentViewHeight = typedArray.getDimensionPixelSize(R.styleable.LinesEditView_classic_contentViewHeight, + dp2px(context, 140)); typedArray.recycle(); // init(); @@ -61,14 +69,17 @@ private void init() { id_et_input = (EditText) view.findViewById(R.id.id_et_input); id_tv_input = (TextView) view.findViewById(R.id.id_tv_input); - if (this.getBackground()==null){ + if (this.getBackground() == null) { this.setBackgroundResource(R.drawable.selector_lines_edit_view_bg); } id_et_input.addTextChangedListener(mTextWatcher); id_et_input.setHint(hintText); + id_et_input.setHintTextColor(hintTextColor); id_et_input.setText(contentText); - id_et_input.setHeight((int) contentHeight); + id_et_input.setTextColor(contentTextColor); + id_et_input.setTextSize(TypedValue.COMPLEX_UNIT_PX, contentTextSize); + id_et_input.setHeight((int) contentViewHeight); /** * 配合 id_tv_input xml的 android:focusable="true" android:focusableInTouchMode="true" @@ -94,7 +105,7 @@ public void onFocusChange(View view, boolean b) { } - private TextWatcher mTextWatcher =new TextWatcher() { + private TextWatcher mTextWatcher = new TextWatcher() { private int editStart; @@ -119,21 +130,21 @@ public void afterTextChanged(Editable editable) { // 先去掉监听器,否则会出现栈溢出 id_et_input.removeTextChangedListener(mTextWatcher); - if (ignoreCnOrEn){ - //当输入字符个数超过限制的大小时,进行截断操作 - while (calculateLengthIgnoreCnOrEn(editable.toString()) > MAX_COUNT) { - editable.delete(editStart - 1, editEnd); - editStart--; - editEnd--; - } - }else{ - // 因为是中英文混合,单个字符而言,calculateLength函数都会返回1 - while (calculateLength(editable.toString()) > MAX_COUNT) { // 当输入字符个数超过限制的大小时,进行截断操作 - editable.delete(editStart - 1, editEnd); - editStart--; - editEnd--; - } - } + if (ignoreCnOrEn) { + //当输入字符个数超过限制的大小时,进行截断操作 + while (calculateLengthIgnoreCnOrEn(editable.toString()) > MAX_COUNT) { + editable.delete(editStart - 1, editEnd); + editStart--; + editEnd--; + } + } else { + // 因为是中英文混合,单个字符而言,calculateLength函数都会返回1 + while (calculateLength(editable.toString()) > MAX_COUNT) { // 当输入字符个数超过限制的大小时,进行截断操作 + editable.delete(editStart - 1, editEnd); + editStart--; + editEnd--; + } + } id_et_input.setSelection(editStart); @@ -158,51 +169,79 @@ private long calculateLength(CharSequence c) { } return Math.round(len); } + private int calculateLengthIgnoreCnOrEn(CharSequence c) { int len = 0; for (int i = 0; i < c.length(); i++) { - len++; + len++; } return len; } private void configCount() { - if (ignoreCnOrEn){ - int nowCount=calculateLengthIgnoreCnOrEn(id_et_input.getText().toString()); + if (ignoreCnOrEn) { + int nowCount = calculateLengthIgnoreCnOrEn(id_et_input.getText().toString()); // - id_tv_input.setText(String.valueOf((MAX_COUNT - nowCount))+"/"+MAX_COUNT); - }else{ - long nowCount=calculateLength(id_et_input.getText().toString()); + id_tv_input.setText(String.valueOf((MAX_COUNT - nowCount)) + "/" + MAX_COUNT); + } else { + long nowCount = calculateLength(id_et_input.getText().toString()); // - id_tv_input.setText(String.valueOf((MAX_COUNT - nowCount))+"/"+MAX_COUNT); + id_tv_input.setText(String.valueOf((MAX_COUNT - nowCount)) + "/" + MAX_COUNT); } } + private static int dp2px(Context context, float dp) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dp * scale + 0.5f); } + public void setContentText(String content) { - contentText=content; - if (id_et_input==null){ + contentText = content; + if (id_et_input == null) { return; } id_et_input.setText(contentText); } + public String getContentText() { - if (id_et_input!=null){ - contentText=id_et_input.getText()==null?"":id_et_input.getText().toString(); + if (id_et_input != null) { + contentText = id_et_input.getText() == null ? "" : id_et_input.getText().toString(); } - return contentText; + return contentText; } public void setHintText(String hintText) { this.hintText = hintText; + if (id_et_input == null) { + return; + } id_et_input.setHint(hintText); } + public void setContentTextSize(int size) { + if (id_et_input == null) { + return; + } + id_et_input.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); + } + + public void setContentTextColor(int color) { + if (id_et_input == null) { + return; + } + id_et_input.setTextColor(color); + } + + public void setHintColor(int color) { + if (id_et_input == null) { + return; + } + id_et_input.setHintTextColor(color); + } + public String getHintText() { - if (id_et_input!=null) { + if (id_et_input != null) { hintText = id_et_input.getHint() == null ? "" : id_et_input.getHint().toString(); } return hintText; diff --git a/library/src/main/res/layout/layout_lines_edit_view.xml b/library/src/main/res/layout/layout_lines_edit_view.xml index 74f89c0..dc06a1f 100644 --- a/library/src/main/res/layout/layout_lines_edit_view.xml +++ b/library/src/main/res/layout/layout_lines_edit_view.xml @@ -2,33 +2,31 @@ + android:focusable="true" + android:focusableInTouchMode="true" + android:orientation="vertical"> + android:textColor="#DE000000" + android:textSize="16dp" /> + android:text="100/100" + android:textColor="#8A000000" + android:textSize="14dp" /> \ No newline at end of file diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index ef02a65..6e6d61b 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -4,7 +4,10 @@ + - + + + \ No newline at end of file