一、检测字符串中是否包含汉字
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(); }
public static boolean isIntentAvailable(Context context, String action) { final PackageManager packageManager = context.getPackageManager(); final Intent intent = new Intent(action); List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); return list.size() > 0; }
private void setImageBitmap(ImageView imageView, Bitmap bitmap) { // Use TransitionDrawable to fade in. final TransitionDrawable td = new TransitionDrawable(new Drawable[] { new ColorDrawable(android.R.color.transparent), new BitmapDrawable(mContext.getResources(), bitmap) }); //noinspection deprecation imageView.setBackgroundDrawable(imageView.getDrawable()); imageView.setImageDrawable(td); td.startTransition(200); }
五、 扫描指定的文件,用途:从本软件新增、修改、删除图片、文件某一个文件(音频、视频)需要更新系统媒体库时使用,不必扫描整个SD卡。
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
public static int dipToPX(final Context ctx, float dip) { return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, ctx.getResources().getDisplayMetrics()); }
private void saveCurrentPosition() { if (mListView != null) { int position = mListView.getFirstVisiblePosition(); View v = mListView.getChildAt(0); int top = (v == null) ? 0 : v.getTop(); //保存position和top } } private void restorePosition() { if (mFolder != null && mListView != null) { int position = 0;//取出保存的数据 int top = 0;//取出保存的数据 mListView.setSelectionFromTop(position, top); } }
八、 文件夹排序(先文件夹排序,后文件排序)
public static void sortFiles(File[] files) { Arrays.sort(files, new Comparator<File>() { @Override public int compare(File lhs, File rhs) { //返回负数表示o1 小于o2,返回0 表示o1和o2相等,返回正数表示o1大于o2。 boolean l1 = lhs.isDirectory(); boolean l2 = rhs.isDirectory(); if (l1 && !l2) return -1; else if (!l1 && l2) return 1; else { return lhs.getName().compareTo(rhs.getName()); } } }); }
@SuppressWarnings("unchecked") public static <T> T[] toArray(Class<?> cls, ArrayList<T> items) { if (items == null || items.size() == 0) { return (T[]) Array.newInstance(cls, 0); } return items.toArray((T[]) Array.newInstance(cls, items.size())); }
public static double getScreenPhysicalSize(Activity ctx) { DisplayMetrics dm = new DisplayMetrics(); ctx.getWindowManager().getDefaultDisplay().getMetrics(dm); double diagonalPixels = Math.sqrt(Math.pow(dm.widthPixels, 2) + Math.pow(dm.heightPixels, 2)); return diagonalPixels / (160 * dm.density); }
public static boolean isTablet(Context context) { return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE; }
public static float GetTextWidth(String text, float Size) { TextPaint FontPaint = new TextPaint(); FontPaint.setTextSize(Size); return FontPaint.measureText(text); }
public static void startApkActivity(final Context ctx, String packageName) { PackageManager pm = ctx.getPackageManager(); PackageInfo pi; try { pi = pm.getPackageInfo(packageName, 0); Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setPackage(pi.packageName); List<ResolveInfo> apps = pm.queryIntentActivities(intent, 0); ResolveInfo ri = apps.iterator().next(); if (ri != null) { String className = ri.activityInfo.name; intent.setComponent(new ComponentName(packageName, className)); ctx.startActivity(intent); } } catch (NameNotFoundException e) { Log.e("startActivity", e); } }