Skip to content

Commit dcbdaf4

Browse files
committed
https://github.com/mylhyl/Android-CircleDialog/issues/32#issuecomment-451403929
1 parent dcb0ee9 commit dcbdaf4

File tree

5 files changed

+93
-5
lines changed

5 files changed

+93
-5
lines changed

app/src/main/java/com/mylhyl/circledialog/sample/MainActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,10 @@ public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
169169
.setInputHeight(300)
170170
.setInputShowKeyboard(true)
171171
.setInputEmoji(true)
172-
.setInputCounter(50)
172+
.setInputCounter(10)
173173
// .setInputCounter(20, (maxLen, currentLen) -> maxLen - currentLen + "/" + maxLen)
174174
.configInput(params -> {
175+
// params.isCounterAllEn = true;
175176
// params.padding = new int[]{30, 30, 30, 30};
176177
// params.inputBackgroundResourceId = R.drawable.bg_input;
177178
// params.gravity = Gravity.CENTER;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.mylhyl.circledialog;
2+
3+
import android.text.Editable;
4+
import android.text.TextUtils;
5+
import android.text.TextWatcher;
6+
import android.widget.EditText;
7+
import android.widget.TextView;
8+
9+
/**
10+
* 中文1字符
11+
* Created by hupei on 2019/01/07 09:36.
12+
*/
13+
public class MaxLengthEnWatcher implements TextWatcher {
14+
private int mMaxLen;
15+
private EditText mEditText;
16+
private TextView mTvCounter;
17+
private CircleParams mParams;
18+
19+
public MaxLengthEnWatcher(int maxLen, EditText editText, TextView textView, CircleParams params) {
20+
this.mMaxLen = maxLen;
21+
this.mEditText = editText;
22+
this.mTvCounter = textView;
23+
this.mParams = params;
24+
if (mEditText != null) {
25+
String defText = mEditText.getText().toString();
26+
int currentLen = maxLen - defText.length();
27+
if (mParams.inputCounterChangeListener != null) {
28+
String counterText = mParams.inputCounterChangeListener
29+
.onCounterChange(maxLen, currentLen);
30+
mTvCounter.setText(counterText == null ? "" : counterText);
31+
} else {
32+
mTvCounter.setText(String.valueOf(currentLen));
33+
}
34+
}
35+
}
36+
37+
@Override
38+
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
39+
40+
}
41+
42+
@Override
43+
public void onTextChanged(CharSequence s, int start, int before, int count) {
44+
45+
}
46+
47+
@Override
48+
public void afterTextChanged(Editable editable) {
49+
int editStart = mEditText.getSelectionStart();
50+
int editEnd = mEditText.getSelectionEnd();
51+
// 先去掉监听器,否则会出现栈溢出
52+
mEditText.removeTextChangedListener(this);
53+
if (!TextUtils.isEmpty(editable)) {
54+
//循环删除多出的字符
55+
while (editable.toString().length() > mMaxLen) {
56+
editable.delete(editStart - 1, editEnd);
57+
editStart--;
58+
editEnd--;
59+
}
60+
}
61+
int currentLen = mMaxLen - editable.toString().length();
62+
if (mParams.inputCounterChangeListener != null) {
63+
String counterText = mParams.inputCounterChangeListener
64+
.onCounterChange(mMaxLen, currentLen);
65+
mTvCounter.setText(counterText == null ? "" : counterText);
66+
} else {
67+
mTvCounter.setText(String.valueOf(currentLen));
68+
}
69+
70+
mEditText.setSelection(editStart);
71+
// 恢复监听器
72+
mEditText.addTextChangedListener(this);
73+
}
74+
}

circledialog/src/main/java/com/mylhyl/circledialog/MaxLengthWatcher.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.widget.TextView;
88

99
/**
10+
* 中文2字符
1011
* Created by hupei on 2018/11/1 11:19.
1112
*/
1213
public class MaxLengthWatcher implements TextWatcher {

circledialog/src/main/java/com/mylhyl/circledialog/params/InputParams.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ public InputParams[] newArray(int size) {
111111
* 是否禁止输入表情
112112
*/
113113
public boolean isEmojiInput;
114+
/**
115+
* 输入限制计数器中文是否算1个字符
116+
*/
117+
public boolean isCounterAllEn;
114118

115119
public InputParams() {
116120
}
@@ -137,6 +141,7 @@ protected InputParams(Parcel in) {
137141
this.counterColor = in.readInt();
138142
this.showSoftKeyboard = in.readByte() != 0;
139143
this.isEmojiInput = in.readByte() != 0;
144+
this.isCounterAllEn = in.readByte() != 0;
140145
}
141146

142147
@Override
@@ -167,5 +172,6 @@ public void writeToParcel(Parcel dest, int flags) {
167172
dest.writeInt(this.counterColor);
168173
dest.writeByte(this.showSoftKeyboard ? (byte) 1 : (byte) 0);
169174
dest.writeByte(this.isEmojiInput ? (byte) 1 : (byte) 0);
175+
dest.writeByte(this.isCounterAllEn ? (byte) 1 : (byte) 0);
170176
}
171177
}

circledialog/src/main/java/com/mylhyl/circledialog/view/BodyInputView.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import com.mylhyl.circledialog.CircleParams;
1414
import com.mylhyl.circledialog.EmojiFilter;
15+
import com.mylhyl.circledialog.MaxLengthEnWatcher;
1516
import com.mylhyl.circledialog.MaxLengthWatcher;
1617
import com.mylhyl.circledialog.params.DialogParams;
1718
import com.mylhyl.circledialog.params.InputParams;
@@ -61,7 +62,8 @@ private void init(Context context, CircleParams params) {
6162
mEditText.setTextColor(inputParams.textColor);
6263
mEditText.addOnLayoutChangeListener(new OnLayoutChangeListener() {
6364
@Override
64-
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
65+
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft
66+
, int oldTop, int oldRight, int oldBottom) {
6567
int height = v.getHeight();
6668
if (inputParams.inputHeight > height) {
6769
mEditText.setHeight(inputParams.inputHeight);
@@ -113,9 +115,13 @@ public void onLayoutChange(View v, int left, int top, int right, int bottom, int
113115
mTvCounter.setTextSize(INPUT_COUNTER__TEXT_SIZE);
114116
mTvCounter.setTextColor(inputParams.counterColor);
115117

116-
mEditText.addTextChangedListener(new MaxLengthWatcher(inputParams.maxLen
117-
, mEditText, mTvCounter, params));
118-
118+
if (inputParams.isCounterAllEn) {
119+
mEditText.addTextChangedListener(new MaxLengthEnWatcher(inputParams.maxLen
120+
, mEditText, mTvCounter, params));
121+
} else {
122+
mEditText.addTextChangedListener(new MaxLengthWatcher(inputParams.maxLen
123+
, mEditText, mTvCounter, params));
124+
}
119125
addView(mTvCounter, layoutParamsCounter);
120126
}
121127

0 commit comments

Comments
 (0)