Skip to content

Commit

Permalink
release 2.6.19
Browse files Browse the repository at this point in the history
  • Loading branch information
hupei committed Sep 29, 2020
1 parent 5e6cfc2 commit 1dbe9aa
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# 引入

```xml
compile 'com.mylhyl:circleDialog:2.6.18'
compile 'com.mylhyl:circleDialog:2.6.19'
```

[下载APK体验](https://fir.im/sbvq)或手机扫描下面二维码
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 字节
*/
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,90 +26,73 @@ 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:自动
*
* @deprecated {@link DialogParams#isManualClose}
*/
@Deprecated
public boolean isManualClose;

/**
* 文本
*/
public String text;

/**
* 内间距 [left, top, right, bottom]
*/
public int[] padding = CircleDimen.INPUT_PADDING;

/**
* 字样式
* {@linkplain Typeface#NORMAL NORMAL}
Expand All @@ -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;
/**
* 输入长度最大限制计数类型
* <p>
* 默认按字节计算
* <p>
* {@link com.mylhyl.circledialog.MaxLengthByteWatcher 0=按字节长度}
* <p>
* {@link com.mylhyl.circledialog.MaxLengthWatcher 1=中文按长度2,其它按长度1算}
* <p>
* {@link com.mylhyl.circledialog.MaxLengthEnWatcher 2=只算英文长度1}
*
* @since 2.6.19
*/
public int maxLengthType;

public InputParams() {
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ ext {
]

publish = [
version: "2.6.18"
version: "2.6.19"
]
}

0 comments on commit 1dbe9aa

Please sign in to comment.