diff --git a/README.md b/README.md index df18a9a..a8fdc0e 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ # 引入 ```xml - compile 'com.mylhyl:circleDialog:2.6.18' + compile 'com.mylhyl:circleDialog:2.6.19' ``` [下载APK体验](https://fir.im/sbvq)或手机扫描下面二维码 diff --git a/circledialog/src/main/java/com/mylhyl/circledialog/MaxLengthByteWatcher.java b/circledialog/src/main/java/com/mylhyl/circledialog/MaxLengthByteWatcher.java new file mode 100644 index 0000000..7e38e0b --- /dev/null +++ b/circledialog/src/main/java/com/mylhyl/circledialog/MaxLengthByteWatcher.java @@ -0,0 +1,88 @@ +package com.mylhyl.circledialog; + +import android.text.Editable; +import android.text.TextUtils; +import android.text.TextWatcher; +import android.widget.EditText; +import android.widget.TextView; + +/** + * Created by hupei on 2020/9/29 19:11. + *

+ * 字节 + */ +public class MaxLengthByteWatcher implements TextWatcher { + private int mMaxLen; + private EditText mEditText; + private TextView mTvCounter; + private CircleParams mParams; + + public MaxLengthByteWatcher(int maxLen, EditText editText, TextView textView, CircleParams params) { + this.mMaxLen = maxLen; + this.mEditText = editText; + this.mTvCounter = textView; + this.mParams = params; + if (mEditText != null) { + String defText = mEditText.getText().toString(); + int currentLen = maxLen - chineseLength(defText); + if (params.inputCounterChangeListener != null) { + String counterText = params.inputCounterChangeListener + .onCounterChange(maxLen, currentLen); + mTvCounter.setText(counterText == null ? "" : counterText); + } else { + mTvCounter.setText(String.valueOf(currentLen)); + } + } + } + + public static int chineseLength(String str) { + int valueLength = 0; + if (!TextUtils.isEmpty(str)) { + for (int i = 0; i < str.length(); i++) { + // 获取一个字符 + String temp = str.substring(i, i + 1); + // bytes长度为 + valueLength += temp.getBytes().length; + } + } + return valueLength; + } + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + + } + + @Override + public void afterTextChanged(Editable editable) { + int editStart = mEditText.getSelectionStart(); + int editEnd = mEditText.getSelectionEnd(); + // 先去掉监听器,否则会出现栈溢出 + mEditText.removeTextChangedListener(this); + if (!TextUtils.isEmpty(editable)) { + //循环删除多出的字符 + while (chineseLength(editable.toString()) > mMaxLen) { + editable.delete(editStart - 1, editEnd); + editStart--; + editEnd--; + } + } + int currentLen = mMaxLen - chineseLength(editable.toString()); + if (mParams.inputCounterChangeListener != null) { + String counterText = mParams.inputCounterChangeListener + .onCounterChange(mMaxLen, currentLen); + mTvCounter.setText(counterText == null ? "" : counterText); + } else { + mTvCounter.setText(String.valueOf(currentLen)); + } + + mEditText.setSelection(editStart); + // 恢复监听器 + mEditText.addTextChangedListener(this); + } +} diff --git a/circledialog/src/main/java/com/mylhyl/circledialog/params/InputParams.java b/circledialog/src/main/java/com/mylhyl/circledialog/params/InputParams.java index f942762..1c98daa 100644 --- a/circledialog/src/main/java/com/mylhyl/circledialog/params/InputParams.java +++ b/circledialog/src/main/java/com/mylhyl/circledialog/params/InputParams.java @@ -26,72 +26,58 @@ public InputParams[] newArray(int size) { return new InputParams[size]; } }; - /** * 输入框与body视图的距离 */ public int[] margins = CircleDimen.INPUT_MARGINS; - /** * 输入框的高度 */ public int inputHeight = CircleDimen.INPUT_HEIGHT; - /** * 输入框提示语 */ public String hintText; - /** * 输入框提示语颜色 */ public int hintTextColor = CircleColor.INPUT_TEXT_HINT; - /** * 输入框背景资源文件 */ public int inputBackgroundResourceId; - /** * 输入框边框线条粗细 */ public int strokeWidth = 1; - /** * 输入框边框线条颜色 */ public int strokeColor = CircleColor.INPUT_STROKE; - /** * 输入框的背景 */ public int inputBackgroundColor; - /** * body视图的背景色 */ public int backgroundColor; - /** * 输入框字体大小 */ public int textSize = CircleDimen.INPUT_TEXT_SIZE; - /** * 输入框字体颜色 */ public int textColor = CircleColor.INPUT_TEXT; - /** * 输入类型 */ public int inputType = InputType.TYPE_NULL; - /** * 文字对齐方式,默认左上角 */ public int gravity = Gravity.LEFT | Gravity.TOP; - /** * 是否触发自动关闭对话框 true:手动;false:自动 * @@ -99,17 +85,14 @@ public InputParams[] newArray(int size) { */ @Deprecated public boolean isManualClose; - /** * 文本 */ public String text; - /** * 内间距 [left, top, right, bottom] */ public int[] padding = CircleDimen.INPUT_PADDING; - /** * 字样式 * {@linkplain Typeface#NORMAL NORMAL} @@ -118,32 +101,45 @@ public InputParams[] newArray(int size) { * {@linkplain Typeface#BOLD_ITALIC BOLD_ITALIC} */ public int styleText = Typeface.NORMAL; - /** * 输入框限制字符数量,如counter=50:中(占2个)英(1个)文总字符数 */ public int maxLen; - /** * 外边距 [右,下] */ public int[] counterMargins = CircleDimen.INPUT_COUNTER_MARGINS; public int counterColor = CircleColor.INPUT_COUNTER_TEXT; - /** * 显示软键盘 */ public boolean showSoftKeyboard; - /** * 是否禁止输入表情 */ public boolean isEmojiInput; - /** * 输入限制计数器中文是否算1个字符 + * + * @see #maxLengthType + * @deprecated since 2.6.19 后无效 */ + @Deprecated public boolean isCounterAllEn; + /** + * 输入长度最大限制计数类型 + *

+ * 默认按字节计算 + *

+ * {@link com.mylhyl.circledialog.MaxLengthByteWatcher 0=按字节长度} + *

+ * {@link com.mylhyl.circledialog.MaxLengthWatcher 1=中文按长度2,其它按长度1算} + *

+ * {@link com.mylhyl.circledialog.MaxLengthEnWatcher 2=只算英文长度1} + * + * @since 2.6.19 + */ + public int maxLengthType; public InputParams() { } @@ -172,6 +168,7 @@ protected InputParams(Parcel in) { this.showSoftKeyboard = in.readByte() != 0; this.isEmojiInput = in.readByte() != 0; this.isCounterAllEn = in.readByte() != 0; + this.maxLengthType = in.readInt(); } @Override @@ -204,5 +201,6 @@ public void writeToParcel(Parcel dest, int flags) { dest.writeByte(this.showSoftKeyboard ? (byte) 1 : (byte) 0); dest.writeByte(this.isEmojiInput ? (byte) 1 : (byte) 0); dest.writeByte(this.isCounterAllEn ? (byte) 1 : (byte) 0); + dest.writeInt(this.maxLengthType); } } diff --git a/circledialog/src/main/java/com/mylhyl/circledialog/view/BodyInputView.java b/circledialog/src/main/java/com/mylhyl/circledialog/view/BodyInputView.java index 571f6e5..f6784af 100644 --- a/circledialog/src/main/java/com/mylhyl/circledialog/view/BodyInputView.java +++ b/circledialog/src/main/java/com/mylhyl/circledialog/view/BodyInputView.java @@ -12,6 +12,7 @@ import com.mylhyl.circledialog.CircleParams; import com.mylhyl.circledialog.Controller; import com.mylhyl.circledialog.EmojiFilter; +import com.mylhyl.circledialog.MaxLengthByteWatcher; import com.mylhyl.circledialog.MaxLengthEnWatcher; import com.mylhyl.circledialog.MaxLengthWatcher; import com.mylhyl.circledialog.params.ButtonParams; @@ -148,12 +149,14 @@ public void onGlobalLayout() { mTvCounter = new ScaleTextView(context); mTvCounter.setTextSize(INPUT_COUNTER__TEXT_SIZE); mTvCounter.setTextColor(inputParams.counterColor); - - if (inputParams.isCounterAllEn) { + if (inputParams.maxLengthType == 1) { + mEditText.addTextChangedListener(new MaxLengthWatcher(inputParams.maxLen + , mEditText, mTvCounter, params)); + } else if (inputParams.maxLengthType == 2) { mEditText.addTextChangedListener(new MaxLengthEnWatcher(inputParams.maxLen , mEditText, mTvCounter, params)); } else { - mEditText.addTextChangedListener(new MaxLengthWatcher(inputParams.maxLen + mEditText.addTextChangedListener(new MaxLengthByteWatcher(inputParams.maxLen , mEditText, mTvCounter, params)); } addView(mTvCounter, layoutParamsCounter); diff --git a/config.gradle b/config.gradle index c05b7df..c561648 100644 --- a/config.gradle +++ b/config.gradle @@ -21,6 +21,6 @@ ext { ] publish = [ - version: "2.6.18" + version: "2.6.19" ] } \ No newline at end of file