字符串按指定字节数截取、隐藏与显示输入软键盘、收起状态栏以及判断服务是否运行功能代码段
2014-12-11 10:01
302 查看
1、字符串中指定字节数截取,以及多余部分显示方式
/**
* 获取字节长度
*
*/
public static int getWordCount(String s) {
s = s.replaceAll("[^\\x00-\\xff]", "**");
int length = s.length();
return length;
}
/**
*
* Description:长度超过3个汉字(6个字符)字符串,自动截取前三个字,多于的已省略号...标识
*
* @param str 要截取的字符串
* @param n 截取的字节数
* @return
*/
public static String getFormatString(String str,int n)
{
//格式化字符串长度,超出部分显示省略号,区分汉字跟字母。汉字2个字节,字母数字一个字节
String temp = "";
if(getWordCount(str)<=n)//如果长度比需要的长度n小,返回原字符串
{
return str;
}
else{
int t=0;
char[] q = str.toCharArray();
for(int i=0;i<q.length&&t<=n;i++)
{
if((int)q[i]>=0x4e00 && (int)q[i]<=0x9fa5)//是否汉字
{
t += 2;
}
else {
t++;
}
// if (t <= n) {//判断如果此时字符数大于要求显示的字符数n,是否取该汉字
temp += q[i];
// }
}
return (temp+"...");
}
}
}
2、隐藏与显示输入软键盘
/**
* 隐藏界面输入软键盘
*
* @param activity
*/
public static void hideSoftInputFromWindow(final Activity activity) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
View view = activity.getCurrentFocus();
if (view != null) {
imm.hideSoftInputFromWindow(view.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
});
}
/**
* 显示界面输入键盘
*
* @param activity
*/
public static void showSoftInputFromWindow(final Activity activity) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
View view = activity.getCurrentFocus();
if (view != null) {
LogUtil.d("showSoftInput");
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
}
});
}
3、收起状态栏的解决方法
/**
* 收起状态栏
*
* @param context
* 上下文对象
* @return 成功收起状态栏返回true,否则返回false
*/
public static boolean collapseStatusBar(Context context) {
Object statusbarService = context.getSystemService("statusbar");
if (statusbarService == null) {
return false;
}
try {
Class<?> statusBarManager = Class
.forName("android.app.StatusBarManager");
if (statusBarManager == null) {
return false;
}
Method collapseMethod;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
collapseMethod = statusBarManager.getMethod("collapsePanels");
} else {
collapseMethod = statusBarManager.getMethod("collapse");
}
if (collapseMethod == null) {
return false;
}
collapseMethod.invoke(statusbarService);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}
4、判断服务是否运行功能
/**
* 用来判断服务是否运行.
*
* @param context
* @param className
* 判断的服务名字
* @return true 在运行 false 不在运行
*/
public static boolean isServiceRunning(Context mContext, String className) {
boolean isRunning = false;
ActivityManager activityManager = (ActivityManager) mContext
.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList = activityManager
.getRunningServices(Integer.MAX_VALUE);
if (!(serviceList.size() > 0)) {
return false;
}
String name = "";
String process = "";
for (int i = 0; i < serviceList.size(); i++) {
name = serviceList.get(i).service.getClassName();
process = serviceList.get(i).process;
if (name.equals(className) == true) {
if (process
.startsWith(BizConstant.NET_PHONE_ACCOUNT_TYPE + ":")) {
isRunning = true;
break;
}
}
}
return isRunning;
}
/**
* 获取字节长度
*
*/
public static int getWordCount(String s) {
s = s.replaceAll("[^\\x00-\\xff]", "**");
int length = s.length();
return length;
}
/**
*
* Description:长度超过3个汉字(6个字符)字符串,自动截取前三个字,多于的已省略号...标识
*
* @param str 要截取的字符串
* @param n 截取的字节数
* @return
*/
public static String getFormatString(String str,int n)
{
//格式化字符串长度,超出部分显示省略号,区分汉字跟字母。汉字2个字节,字母数字一个字节
String temp = "";
if(getWordCount(str)<=n)//如果长度比需要的长度n小,返回原字符串
{
return str;
}
else{
int t=0;
char[] q = str.toCharArray();
for(int i=0;i<q.length&&t<=n;i++)
{
if((int)q[i]>=0x4e00 && (int)q[i]<=0x9fa5)//是否汉字
{
t += 2;
}
else {
t++;
}
// if (t <= n) {//判断如果此时字符数大于要求显示的字符数n,是否取该汉字
temp += q[i];
// }
}
return (temp+"...");
}
}
}
2、隐藏与显示输入软键盘
/**
* 隐藏界面输入软键盘
*
* @param activity
*/
public static void hideSoftInputFromWindow(final Activity activity) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
View view = activity.getCurrentFocus();
if (view != null) {
imm.hideSoftInputFromWindow(view.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
});
}
/**
* 显示界面输入键盘
*
* @param activity
*/
public static void showSoftInputFromWindow(final Activity activity) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
View view = activity.getCurrentFocus();
if (view != null) {
LogUtil.d("showSoftInput");
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
}
});
}
3、收起状态栏的解决方法
/**
* 收起状态栏
*
* @param context
* 上下文对象
* @return 成功收起状态栏返回true,否则返回false
*/
public static boolean collapseStatusBar(Context context) {
Object statusbarService = context.getSystemService("statusbar");
if (statusbarService == null) {
return false;
}
try {
Class<?> statusBarManager = Class
.forName("android.app.StatusBarManager");
if (statusBarManager == null) {
return false;
}
Method collapseMethod;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
collapseMethod = statusBarManager.getMethod("collapsePanels");
} else {
collapseMethod = statusBarManager.getMethod("collapse");
}
if (collapseMethod == null) {
return false;
}
collapseMethod.invoke(statusbarService);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}
4、判断服务是否运行功能
/**
* 用来判断服务是否运行.
*
* @param context
* @param className
* 判断的服务名字
* @return true 在运行 false 不在运行
*/
public static boolean isServiceRunning(Context mContext, String className) {
boolean isRunning = false;
ActivityManager activityManager = (ActivityManager) mContext
.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList = activityManager
.getRunningServices(Integer.MAX_VALUE);
if (!(serviceList.size() > 0)) {
return false;
}
String name = "";
String process = "";
for (int i = 0; i < serviceList.size(); i++) {
name = serviceList.get(i).service.getClassName();
process = serviceList.get(i).process;
if (name.equals(className) == true) {
if (process
.startsWith(BizConstant.NET_PHONE_ACCOUNT_TYPE + ":")) {
isRunning = true;
break;
}
}
}
return isRunning;
}
相关文章推荐
- 类库------对一些字符串进行操作的类1.判断输入是否数字2.截取字符串函数3.过滤输入信息4.生成随机数5.生成验证码图片6.获取汉字第一个拼音7.半角转全角8.全角转半角
- 4.设计一个Email邮箱注册应用程序。要求:用户输入完成单击“立即注册”按,判断“密码”和“确认密码”文本框内容是否一致,如果一致在立即注册按钮上方显示用户输入的邮件地址,运行结果如图所示。
- c# 判断字符是否是全角, 获取字符串的字节数 , 获取字符串指定长度字节数的字符串
- 判断全角半角混合字符串的字节数是否超过指定的字节数。(半角被认为是一个字节)
- 监测程序详细介绍,快捷键的设置;txt文件里面一行行的读取、删除、添加字符串等;根据程序路径名以及程序名判断程序是否在运行;
- 判断状态栏是否显示以及获取状态栏高度的方法
- 20141020个人日志(mac下显示隐藏文件方法,判断字符串是否为数字的方法)
- Java文本框只能输入数字,键盘监听,以及判断字符串是否只是数字,并去除前面多余的0
- C判断字符输入是否为指定字符串
- Android Bitmap的截取及状态栏的隐藏和显示功能
- @V@ java代码笔记2010-06-12:java控制台输入各类型类实现;以及判断输入字符串里面是否有数字的两种方法:方法1:转换成字符数组;方法2:正则表达式。
- 【总结】DateTime日期类型格式化显示(转)以及判断输入文本框是否符合日期格式
- c#数字验证码功能,以及判断用户输入是否正确。
- js 判断 checked 是否选中 js 删除指定字符串 ,js 截取指定位图的字符串
- Android——显示当前运行所有服务,判断服务是否运行
- 判断手机号的输入是否正确、获取当前的时间以及显示距离上次发布有多久的时间
- ASP.NET中服务器控件Button调用javascript代码运用正则表达式验证TextBox中输入的是否为正整数或正小数,符合要求则继续运行服务器端代码功能的实现
- C# 判断指定字符串是否包含有汉字
- C#判断输入的字符串中是否含有大写字母,小写字母,数字,字符
- 遍历字符串判断指定字符串是否存在