Skip to content

Commit 06b792a

Browse files
committed
开放input工具
1 parent 7a85e64 commit 06b792a

File tree

6 files changed

+168
-280
lines changed

6 files changed

+168
-280
lines changed

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

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.mylhyl.circledialog;
2+
3+
import android.text.InputFilter;
4+
import android.text.Spanned;
5+
6+
import java.util.regex.Pattern;
7+
8+
/**
9+
* Created by hupei on 2018/11/1 11:21.
10+
*/
11+
public class EmojiFilter implements InputFilter {
12+
Pattern emojiPattern = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
13+
Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
14+
15+
public EmojiFilter() {
16+
}
17+
18+
@Override
19+
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
20+
if (this.emojiPattern.matcher(source).find() || containsEmoji(source)) {
21+
return "";
22+
}
23+
return source;
24+
}
25+
26+
/**
27+
* 检测是否有emoji表情
28+
*
29+
* @param source
30+
* @return
31+
*/
32+
private static boolean containsEmoji(CharSequence source) {
33+
int len = source.length();
34+
for (int i = 0; i < len; i++) {
35+
char codePoint = source.charAt(i);
36+
if (!isEmojiCharacter(codePoint)) { //如果不能匹配,则该字符是Emoji表情
37+
return true;
38+
}
39+
}
40+
return false;
41+
}
42+
43+
/**
44+
* 判断是否是Emoji
45+
*
46+
* @param codePoint 比较的单个字符
47+
* @return
48+
*/
49+
private static boolean isEmojiCharacter(char codePoint) {
50+
return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) ||
51+
(codePoint == 0xD) || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
52+
((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || ((codePoint >= 0x10000)
53+
&& (codePoint <= 0x10FFFF));
54+
}
55+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
* Created by hupei on 2018/11/1 11:19.
11+
*/
12+
public class MaxLengthWatcher implements TextWatcher {
13+
private int mMaxLen;
14+
private EditText mEditText;
15+
private TextView mTvCounter;
16+
private CircleParams mParams;
17+
18+
public MaxLengthWatcher(int maxLen, EditText editText, TextView textView, CircleParams params) {
19+
this.mMaxLen = maxLen;
20+
this.mEditText = editText;
21+
this.mTvCounter = textView;
22+
this.mParams = params;
23+
if (mEditText != null) {
24+
String defText = mEditText.getText().toString();
25+
int currentLen = maxLen - chineseLength(defText);
26+
if (mParams.inputCounterChangeListener != null) {
27+
String counterText = mParams.inputCounterChangeListener
28+
.onCounterChange(maxLen, currentLen);
29+
mTvCounter.setText(counterText == null ? "" : counterText);
30+
} else {
31+
mTvCounter.setText(String.valueOf(currentLen));
32+
}
33+
}
34+
}
35+
36+
private int chineseLength(String str) {
37+
int valueLength = 0;
38+
if (!TextUtils.isEmpty(str)) {
39+
// 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1
40+
for (int i = 0; i < str.length(); i++) {
41+
// 获取一个字符
42+
String temp = str.substring(i, i + 1);
43+
// 判断是否为中文字符
44+
if (isChinese(temp)) {
45+
// 中文字符长度为2
46+
valueLength += 2;
47+
} else {
48+
// 其他字符长度为1
49+
valueLength += 1;
50+
}
51+
}
52+
}
53+
return valueLength;
54+
}
55+
56+
private boolean isChinese(String str) {
57+
Boolean isChinese = true;
58+
String chinese = "[\u0391-\uFFE5]";
59+
if (!TextUtils.isEmpty(str)) {
60+
// 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1
61+
for (int i = 0; i < str.length(); i++) {
62+
// 获取一个字符
63+
String temp = str.substring(i, i + 1);
64+
// 判断是否为中文字符
65+
if (temp.matches(chinese)) {
66+
} else {
67+
isChinese = false;
68+
}
69+
}
70+
}
71+
return isChinese;
72+
}
73+
74+
@Override
75+
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
76+
77+
}
78+
79+
@Override
80+
public void onTextChanged(CharSequence s, int start, int before, int count) {
81+
82+
}
83+
84+
@Override
85+
public void afterTextChanged(Editable editable) {
86+
int editStart = mEditText.getSelectionStart();
87+
int editEnd = mEditText.getSelectionEnd();
88+
// 先去掉监听器,否则会出现栈溢出
89+
mEditText.removeTextChangedListener(this);
90+
if (!TextUtils.isEmpty(editable)) {
91+
//循环删除多出的字符
92+
while (chineseLength(editable.toString()) > mMaxLen) {
93+
editable.delete(editStart - 1, editEnd);
94+
editStart--;
95+
editEnd--;
96+
}
97+
}
98+
int currentLen = mMaxLen - chineseLength(editable.toString());
99+
if (mParams.inputCounterChangeListener != null) {
100+
String counterText = mParams.inputCounterChangeListener
101+
.onCounterChange(mMaxLen, currentLen);
102+
mTvCounter.setText(counterText == null ? "" : counterText);
103+
} else {
104+
mTvCounter.setText(String.valueOf(currentLen));
105+
}
106+
107+
mEditText.setSelection(editStart);
108+
// 恢复监听器
109+
mEditText.addTextChangedListener(this);
110+
}
111+
}
Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,21 @@
11
package com.mylhyl.circledialog;
22

3-
import android.annotation.TargetApi;
43
import android.app.Activity;
5-
import android.content.Context;
64
import android.content.res.Configuration;
75
import android.content.res.Resources;
8-
import android.os.Build;
96
import android.util.DisplayMetrics;
10-
import android.util.TypedValue;
11-
import android.view.ViewConfiguration;
12-
13-
import java.lang.reflect.Method;
147

158
final class SystemBarConfig {
169

1710
private static final String STATUS_BAR_HEIGHT_RES_NAME = "status_bar_height";
18-
private static final String NAV_BAR_HEIGHT_RES_NAME = "navigation_bar_height";
19-
private static final String NAV_BAR_HEIGHT_LANDSCAPE_RES_NAME = "navigation_bar_height_landscape";
20-
private static final String SHOW_NAV_BAR_RES_NAME = "config_showNavigationBar";
2111

2212
private final int mStatusBarHeight;
23-
private final int mActionBarHeight;
24-
private final boolean mHasNavigationBar;
25-
private final int mNavigationBarHeight;
26-
private final boolean mInPortrait;
2713
private final int mScreenWidth;
2814
private final int mScreenHeight;
2915

3016
public SystemBarConfig(Activity activity) {
3117
Resources res = activity.getResources();
32-
mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
3318
mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME);
34-
mActionBarHeight = getActionBarHeight(activity);
35-
mNavigationBarHeight = getNavigationBarHeight(activity);
36-
mHasNavigationBar = (mNavigationBarHeight > 0);
3719

3820
DisplayMetrics metrics = new DisplayMetrics();
3921
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
@@ -51,77 +33,6 @@ private int getInternalDimensionSize(Resources res, String key) {
5133
return result;
5234
}
5335

54-
//通过此方法获取action bar的高度
55-
@TargetApi(14)
56-
private int getActionBarHeight(Context context) {
57-
int result = 0;
58-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
59-
TypedValue tv = new TypedValue();
60-
context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
61-
result = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
62-
}
63-
return result;
64-
}
65-
66-
//通过此方法获取navigation bar的高度
67-
@TargetApi(14)
68-
public int getNavigationBarHeight(Context context) {
69-
Resources res = context.getResources();
70-
int result = 0;
71-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
72-
if (hasNavBar(context)) {
73-
String key;
74-
if (mInPortrait) {
75-
key = NAV_BAR_HEIGHT_RES_NAME;
76-
} else {
77-
key = NAV_BAR_HEIGHT_LANDSCAPE_RES_NAME;
78-
}
79-
return getInternalDimensionSize(res, key);
80-
}
81-
}
82-
return result;
83-
}
84-
85-
//通过此方法判断是否存在navigation bar
86-
@TargetApi(14)
87-
private boolean hasNavBar(Context context) {
88-
Resources res = context.getResources();
89-
int resourceId = res.getIdentifier(SHOW_NAV_BAR_RES_NAME, "bool", "android");
90-
if (resourceId != 0) {
91-
boolean hasNav = res.getBoolean(resourceId);
92-
// 查看是否有通过系统属性来控制navigation bar。
93-
if ("1".equals(getNavBarOverride())) {
94-
hasNav = false;
95-
} else if ("0".equals(getNavBarOverride())) {
96-
hasNav = true;
97-
}
98-
return hasNav;
99-
} else {
100-
//可通过此方法来查看设备是否存在物理按键(menu,back,home键)。
101-
return !ViewConfiguration.get(context).hasPermanentMenuKey();
102-
}
103-
}
104-
105-
// 安卓系统允许修改系统的属性来控制navigation bar的显示和隐藏,此方法用来判断是否有修改过相关属性。
106-
// (修改系统文件,在build.prop最后加入qemu.hw.mainkeys=1即可隐藏navigation bar)
107-
// 相关属性模拟器中有使用。
108-
// 当返回值等于"1"表示隐藏navigation bar,等于"0"表示显示navigation bar。
109-
@TargetApi(19)
110-
private String getNavBarOverride() {
111-
String isNavBarOverride = null;
112-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
113-
try {
114-
Class c = Class.forName("android.os.SystemProperties");
115-
Method m = c.getDeclaredMethod("get", String.class);
116-
m.setAccessible(true);
117-
isNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");
118-
} catch (Throwable e) {
119-
isNavBarOverride = null;
120-
}
121-
}
122-
return isNavBarOverride;
123-
}
124-
12536
public int[] getScreenSize() {
12637
return new int[]{mScreenWidth, mScreenHeight};
12738
}
@@ -143,31 +54,4 @@ public int getStatusBarHeight() {
14354
return mStatusBarHeight;
14455
}
14556

146-
/**
147-
* 获取action bar的高度
148-
*
149-
* @return action bar高度的像素值
150-
*/
151-
public int getActionBarHeight() {
152-
return mActionBarHeight;
153-
}
154-
155-
/**
156-
* 判断此设备是否有navigation bar虚拟按键栏
157-
*
158-
* @return true表示有,false表示无
159-
*/
160-
public boolean hasNavigtionBar() {
161-
return mHasNavigationBar;
162-
}
163-
164-
/**
165-
* 获取navigation bar虚拟按键栏的高度
166-
*
167-
* @return 返回navigation bar虚拟按键栏的高度的像素值,如果设备没有navigation bar虚拟按键栏则返回0
168-
*/
169-
public int getNavigationBarHeight() {
170-
return mNavigationBarHeight;
171-
}
172-
17357
}

0 commit comments

Comments
 (0)