27983dbe
孙向锦
project init
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
package com.sunvote.xpadapp.utils;
import android.content.Context;
import android.text.TextPaint;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by Administrator on 2017/6/5.
*/
public class StringUtil {
//正整数、正浮点
private static String numRegex = "^[0-9][0-9]*$|^[0-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*$";
/**
* 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串,返回true
* @param input
* @return boolean
*/
public static boolean isEmpty(String input) {
if (input == null || "".equals(input) || input.isEmpty() || "null".equals(input))
return true;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
return false;
}
}
return true;
}
private static boolean isMatch(String regex, String orginal){
if (StringUtil.isEmpty(orginal)) {
return false;
}
Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(orginal);
return matcher.matches();
}
/**
* 正整数、正浮点
* @param orginal
* 验证字符
* @return
*/
public static boolean isPositiveDecimal(String orginal)
{
return isMatch(numRegex,orginal);
}
/**
* 判断整数位跟小数位
* @param keyInputNum
* 输入数
* @param mIntNumLength
* 整数位判断
* @param mIntDecNumLength
* 小数位判断
* @return
*/
public static boolean isDecimal(String keyInputNum,int mIntNumLength,int mIntDecNumLength){
boolean isOk = true;
int intNumLength = StringUtil.getIntNumLength(keyInputNum);
int decNumLength = StringUtil.getDecNumLength(keyInputNum);
if(mIntNumLength == 0){ //不允许输入整数
isOk = false;
}else{
if(intNumLength > mIntNumLength){
isOk = false;
}
}
if(keyInputNum.contains(".")){
if(mIntDecNumLength == 0){ //不允许输入整数
isOk = false;
}else{
if(decNumLength >mIntDecNumLength ){
isOk = false;
}
}
}
return isOk;
}
/**
* 获取小数的整数位长度
* @param str
* @return
*/
public static int getIntNumLength(String str){
String strTemp="";
if(str.contains(".")){
String [] strArray=str.split(".");
int index = str.indexOf(".");
strTemp=str.substring(0,index);
}else{
strTemp=str;
}
return strTemp.length();
}
/**
* 获取小数的小数位长度
* @param str
* @return
*/
public static int getDecNumLength(String str){
String strTemp="";
if(str.contains(".")){
String [] strArray=str.split(".");
int index = str.indexOf(".");
strTemp=str.substring(index+1,str.length());
}
return strTemp.length();
}
/**
* 根据字节计算宽度 2个字节代表一个汉子
* @return
*/
public static int getMaxTextViewWidth(Context context,int length, float textSize){
float maxTextViewWidth = 0;
String testStr="";
for(int i = 0; i < length;i++){
testStr+="#";
}
maxTextViewWidth = measureTextViewWidth(context,testStr,textSize);
return (int)maxTextViewWidth;
}
// 计算文字的长度(像素)
public static float measureTextViewWidth(Context context, String text, float textSize) {
TextPaint textPaint = new TextPaint();
float mTextSize = context.getResources().getDisplayMetrics().scaledDensity;
textPaint.setTextSize(textSize * mTextSize);
return textPaint.measureText(text);
}
}
|