字符串按指定字节数截取、隐藏与显示输入软键盘、收起状态栏以及判断服务是否运行功能代码段
来源:互联网 发布:出世 入世 知乎 编辑:程序博客网 时间:2024/06/05 14:35
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; }
0 0
- 字符串按指定字节数截取、隐藏与显示输入软键盘、收起状态栏以及判断服务是否运行功能代码段
- JAVA按指定的字节数截取字符串
- 按字节数截取字符串
- Android收起软键盘,隐藏软键盘
- 判断全角半角混合字符串的字节数是否超过指定的字节数。(半角被认为是一个字节)
- 输入为一个字符串和字节数,输出为按字节截取的字符串
- 输入为一个字符串和字节数,输出为按字节截取的字符串
- 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串
- 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。
- 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。
- 字符串截取(按指定的字节数)汉字不可以截取半个
- 按字节截取字符串代码
- Java: 按字节的开始位置来截取指定字节数长度的字符串
- 显示与隐藏软键盘
- 代码片段记录-------隐藏与显示android软键盘
- 判断状态栏是否显示以及获取状态栏高度的方法
- 判断软键盘,显示或隐藏
- 判断软键盘显示还是隐藏
- win7下的输入telnet
- 超全的matlab绘图实例及代码(曲线,曲面,饼状图,柱形图,网格图,球面等)
- 编程之美读书笔记2.8—找符合条件的整数
- Linux下SVN部署Web版本同步
- 【PyQt4 实例30】改变鼠标形状
- 字符串按指定字节数截取、隐藏与显示输入软键盘、收起状态栏以及判断服务是否运行功能代码段
- Hadoop FileSystem文件系统的概要学习
- 同步 与 异步
- Linux 安装 VMware tools方法
- genymotion 的安装配置
- Eclipse SVN插件忽略某些文件或文件夹方法
- cocos2d-x-3.3rc2-001-hello-world
- 数据库生成脚本后,文件太大,无法打开 解决方案(转)
- 策略模式