Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
classichu committed Mar 17, 2017
1 parent 18fc3c6 commit 9c77326
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 59 deletions.
133 changes: 86 additions & 47 deletions library/src/main/java/com/classichu/lineseditview/LinesEditView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,42 +17,48 @@
* ignoreCnOrEn 为false的时候
* 1个中文算1个
* 2个英文算1个
*
* <p>
* 另外:如:只有一个英文时也算1个
* <p>
* 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();
Expand All @@ -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"
Expand All @@ -94,7 +105,7 @@ public void onFocusChange(View view, boolean b) {

}

private TextWatcher mTextWatcher =new TextWatcher() {
private TextWatcher mTextWatcher = new TextWatcher() {

private int editStart;

Expand All @@ -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);

Expand All @@ -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;
Expand Down
20 changes: 9 additions & 11 deletions library/src/main/res/layout/layout_lines_edit_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">

<EditText
android:id="@+id/id_et_input"
android:textSize="16dp"
android:textColor="#DE000000"
android:background="@android:color/transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:gravity="top"
android:hint="请输入文字描述…"
android:inputType="textMultiLine"
android:padding="10dp"
android:selectAllOnFocus="true"
/>
android:textColor="#DE000000"
android:textSize="16dp" />

<TextView
android:id="@+id/id_tv_input"
android:textSize="14dp"
android:textColor="#8A000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginBottom="5dp"
android:layout_marginRight="7dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="100/100"/>
android:text="100/100"
android:textColor="#8A000000"
android:textSize="14dp" />
</LinearLayout>
5 changes: 4 additions & 1 deletion library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
<attr name="classic_maxCount" format="integer"/>
<attr name="classic_ignoreCnOrEn" format="boolean"/>
<attr name="classic_hintText" format="string|reference"/>
<attr name="classic_hintTextColor" format="color|reference"/>
<attr name="classic_contentText" format="string|reference"/>
<attr name="classic_contentHeight" format="dimension|reference"/>
<attr name="classic_contentTextSize" format="dimension|reference"/>
<attr name="classic_contentTextColor" format="color|reference"/>
<attr name="classic_contentViewHeight" format="dimension|reference"/>
</declare-styleable>
</resources>

0 comments on commit 9c77326

Please sign in to comment.