记录常用代码片,以备不时之需..wkakak,开始:
1、 精确获取屏幕尺寸(例如:3.5、4.0、5.0寸屏幕)
1 public static double getScreenPhysicalSize(Activity ctx) { 2 DisplayMetrics dm = new DisplayMetrics(); 3 ctx.getWindowManager().getDefaultDisplay().getMetrics(dm); 4 double diagonalPixels = Math.sqrt(Math.pow(dm.widthPixels, 2) + Math.pow(dm.heightPixels, 2)); 5 return diagonalPixels / (160 * dm.density); 6 }
一般小于7寸的,都是非平板,属于正常智能机系列
2、 判断是否是平板(官方用法)
public static boolean isTablet(Context context) { return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_L ARGE; }
3、 文字根据状态更改颜色 android:textColor
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#53c1bd" android:state_selected="true"/> <item android:color="#53c1bd" android:state_focused="true"/> <item android:color="#53c1bd" android:state_pressed="true"/> <item android:color="#777777"/> </selector>
放在工程 res/color/目录下
4、背景色根据状态更改颜色 android:backgroup
1 <selector xmlns:android="http://schemas.android.com/apk/res/android"> 2 3 <item android:state_selected="true"><shape> 4 <gradient android:angle="0" android:centerColor="#00a59f" android:endColor="#00a59f" android:startColor="#00a59f" /> 5 </shape></item> 6 <item android:state_focused="true"><shape> 7 <gradient android:angle="0" android:centerColor="#00a59f" android:endColor="#00a59f" android:startColor="#00a59f" /> 8 </shape></item> 9 <item android:state_pressed="true"><shape> 10 <gradient android:angle="0" android:centerColor="#00a59f" android:endColor="#00a59f" android:startColor="#00a59f" /> 11 </shape></item> 12 <item><shape> 13 <gradient android:angle="0" android:centerColor="#00ff00" android:endColor="00ff00" android:startColor="00ff00" /> 14 </shape></item> 15 16 </selector>
如果直接给背景色color会报错。
5、 启动APK的默认Activity
1 public static void startApkActivity(final Context ctx, String packageName) { 2 PackageManager pm = ctx.getPackageManager(); 3 PackageInfo pi; 4 try { 5 pi = pm.getPackageInfo(packageName, 0); 6 Intent intent = new Intent(Intent.ACTION_MAIN, null); 7 intent.addCategory(Intent.CATEGORY_LAUNCHER); 8 intent.setPackage(pi.packageName); 9 10 List<ResolveInfo> apps = pm.queryIntentActivities(intent, 0); 11 12 ResolveInfo ri = apps.iterator().next(); 13 if (ri != null) { 14 String className = ri.activityInfo.name; 15 intent.setComponent(new ComponentName(packageName, className)); 16 ctx.startActivity(intent); 17 } 18 } catch (NameNotFoundException e) { 19 Log.e("startActivity", e); 20 } 21 }
7、计算字宽
1 public static float GetTextWidth(String text, float Size) { 2 TextPaint FontPaint = new TextPaint(); 3 FontPaint.setTextSize(Size); 4 return FontPaint.measureText(text); 5 }
8、获取应用程序下所有Activity
1 public static ArrayList<String> getActivities(Context ctx) { 2 ArrayList<String> result = new ArrayList<String>(); 3 Intent intent = new Intent(Intent.ACTION_MAIN, null); 4 intent.setPackage(ctx.getPackageName()); 5 for (ResolveInfo info : ctx.getPackageManager().queryIntentActivities(intent, 0)) { 6 result.add(info.activityInfo.name); 7 } 8 return result; 9 }
9、检测字符串中是否包含汉字
public static boolean checkChinese(String sequence) { final String format = "[\\u4E00-\\u9FA5\\uF900-\\uFA2D]"; boolean result = false; Pattern pattern = Pattern.compile(format); Matcher matcher = pattern.matcher(sequence); result = matcher.find(); return result; }
检测字符串中只能包含:中文、数字、下划线(_)、横线(-)
public static boolean checkNickname(String sequence) {
final String format = "[^\\u4E00-\\u9FA5\\uF900-\\uFA2D\\w-_]";
Pattern pattern = Pattern.compile(format);
Matcher matcher = pattern.matcher(sequence);
return !matcher.find();
}
10、检查有没有应用程序来接受处理你发出的intent
1 public static boolean isIntentAvailable(Context context, String action) { 2 final PackageManager packageManager = context.getPackageManager(); 3 final Intent intent = new Intent(action); 4 List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); 5 return list.size() > 0; 6 }
参考:http://www.cnblogs.com/over140/archive/2013/03/05/2706068.html