【移动应用开发技术】文本工具类TextUtil_第1页
【移动应用开发技术】文本工具类TextUtil_第2页
【移动应用开发技术】文本工具类TextUtil_第3页
【移动应用开发技术】文本工具类TextUtil_第4页
【移动应用开发技术】文本工具类TextUtil_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】文本工具类TextUtil

package

com.example.util;

import

android.text.InputFilter;

import

android.text.Spanned;

import

android.text.TextUtils;

import

android.text.TextWatcher;

import

android.view.View;

import

android.widget.EditText;

/////////////////////////

import

android.graphics.Color;

import

android.text.Spannable;

import

android.text.SpannableString;

import

android.text.Spanned;

import

android.text.style.AbsoluteSizeSpan;

import

android.text.style.ForegroundColorSpan;

import

com.example.base.BaseApplication;

/**

*

文本工具类

*

*

@author

wangzengyang@

*/

public

class

TextUtil

{

/**

*

Returns

true

if

the

string

is

null

or

0-length.

*

*

@param

str

*

the

string

to

be

examined

*

@return

true

if

str

is

null

or

zero

length

*/

public

static

boolean

isEmpty(String

str)

{

if

(str

==

null)

{

return

true;

}

str

=

str.trim();

return

str.length()

==

0

||

str.equals("null");

}

/**

*

去掉文件名称中的非法字符

*

*

@param

str

*

@return

*/

public

static

String

escapeFileName(String

str)

{

if

(str

==

null)

{

return

null;

}

/**

非法字符包括:/\:*?"<>|

*/

StringBuilder

builder

=

new

StringBuilder();

for

(int

i

=

0;

i

<

str.length();

i++)

{

char

c

=

str.charAt(i);

if

(c

==

'/'

||

c

==

'\\'

||

c

==

':'

||

c

==

'*'

||

c

==

'?'

||

c

==

'"'

||

c

==

'<'

||

c

==

'>'

||

c

==

'|')

{

continue;

}

else

{

builder.append(c);

}

}

return

builder.toString();

}

/**

*

从url获取当前图片的id,如果url以ignoreTag开头则直接返回该url;如果ignoreTag为空,则不会判断ignoreTag

*

*

@param

ignoreTag

*

@param

url

*

@return

*/

public

static

String

getIdFromUrl(String

url,

String

ignoreTag)

{

if

(TextUtils.isEmpty(url)

||

(!TextUtils.isEmpty(ignoreTag))

&&

url.startsWith(ignoreTag))

return

url;

int

lastIndex

=

url.lastIndexOf(".jpg");

if

(lastIndex

<

0)

lastIndex

=

url.length()

-

1;

int

beginIndex

=

url.lastIndexOf("/")

+

1;

int

slashIndex

=

url.lastIndexOf("%2F")

+

3;

int

finalSlashIndex

=

url.lastIndexOf("%252F")

+

5;

beginIndex

=

Math.max(Math.max(beginIndex,

slashIndex),

finalSlashIndex);

return

url.substring(beginIndex,

lastIndex);

}

public

static

String

getIdFromUrl(String

url)

{

return

getIdFromUrl(url,

null);

}

public

static

String

trim(String

str)

{

if

(isEmpty(str))

return

null;

return

str.trim();

}

/**

*

从字符串资源文件读取字符串

*

*

@param

resId

*

@return

*/

public

static

String

getString(int

resId)

{

return

BaseApplication.getAppContext().getResources().getString(resId);

}

public

static

CharSequence

getString(int

resIdX,

int

resIdY)

{

return

getString(resIdX,

getString(resIdY));

}

/**

*

从字符串资源文件读取字符串

*

*

@param

resId

*

@param

formatArgs

*

@return

*/

public

static

String

getString(int

resId,

Object...

formatArgs)

{

return

BaseApplication.getAppContext().getResources().getString(resId,

formatArgs);

}

/**

*

比较两个字符串是否相同

*

*

@param

first

*

@param

second

*

@return

*/

public

static

boolean

equals(String

first,

String

second)

{

if

(isEmpty(first)

||

isEmpty(second))

return

false;

return

first.equals(second);

}

/**

简单判断坐标经纬度是否合法

*/

public

static

boolean

isCoordinateEmpty(String

l)

{

if

(l

==

null)

{

return

true;

}

l

=

l.trim();

return

l.length()

==

0

||

l.equals("null")

||

l.equals("0");

}

/**

*

清理密码<br>

*

将密码字符串中的中文、空格去掉

*

*

@param

password

*

@return

*/

public

static

String

cleanPassword(String

password)

{

if

(isEmpty(password))

return

"";

return

password.replaceAll("[^\\x00-\\xff]*|\\s*",

"");

}

/**

*

将密码输入框中的全角字符、空格过滤掉

*

*

@param

editText

*

@param

textWatcher

*/

public

static

void

cleanPasswordEditText(final

EditText

editText,

final

TextWatcher

textWatcher)

{

Object

tag

=

editText.getTag();

if

(tag

!=

null)

{

int

selectionTag

=

0;

try

{

selectionTag

=

(Integer)

tag;

}

catch

(ClassCastException

e)

{

return;

}

editText.setSelection(selectionTag);

editText.setTag(null);

return;

}

String

password

=

editText.getText().toString();

int

selection

=

editText.getSelectionStart();

int

preLength

=

password.length();

password

=

TextUtil.cleanPassword(password);

int

cleanedLength

=

password.length();

selection

=

selection

-

(preLength

-

cleanedLength);

if

(selection

<

0)

selection

=

0;

editText.setTag(selection);

editText.setText(password);

}

/**

*

为EditText

设置密码过滤器

*

*

@param

editText

*/

public

static

void

setPasswordFilter(EditText

editText)

{

InputFilter

lengthfilter

=

new

InputFilter()

{

@Override

public

CharSequence

filter(CharSequence

source,

int

start,

int

end,

Spanned

dest,

int

dstart,

int

dend)

{

return

cleanPassword(source.toString());

}

};

editText.setFilters(new

InputFilter[]

{

lengthfilter

});

}

public

static

int

length(String

phone)

{

return

phone

==

null

?

0

:

phone.length();

}

public

static

String

getIdString(View

v)

{

return

String.valueOf(v.getId());

}

public

static

String[]

getStringArray(int

arrayResId)

{

return

BaseApplication.getAppContext().getResources().getStringArray(arrayResId);

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//unicode转换成中文

public

static

final

String

decodeUnicode(String

theString)

{

char

aChar;

int

len

=

theString.length();

StringBuffer

outBuffer

=

new

StringBuffer(len);

for

(int

x

=

0;

x

<

len;)

{

aChar

=

theString.charAt(x++);

if

(aChar

==

'\\')

{

aChar

=

theString.charAt(x++);

if

(aChar

==

'u')

{

//

Read

the

xxxx

int

value

=

0;

for

(int

i

=

0;

i

<

4;

i++)

{

aChar

=

theString.charAt(x++);

switch

(aChar)

{

case

'0':

case

'1':

case

'2':

case

'3':

case

'4':

case

'5':

case

'6':

case

'7':

case

'8':

case

'9':

value

=

(value

<<

4)

+

aChar

-

'0';

break;

case

'a':

case

'b':

case

'c':

case

'd':

case

'e':

case

'f':

value

=

(value

<<

4)

+

10

+

aChar

-

'a';

break;

case

'A':

case

'B':

case

'C':

case

'D':

case

'E':

case

'F':

value

=

(value

<<

4)

+

10

+

aChar

-

'A';

break;

default:

throw

new

IllegalArgumentException(

"Malformed

\\uxxxx

encoding.");

}

}

outBuffer.append((char)

value);

}

else

{

if

(aChar

==

't')

aChar

=

'\t';

else

if

(aChar

==

'r')

aChar

=

'\r';

else

if

(aChar

==

'n')

aChar

=

'\n';

else

if

(aChar

==

'f')

aChar

=

'\f';

outBuffer.append(aChar);

}

}

else

outBuffer.a

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论