1.格式化价格,这个经常在计算费用精度的时候用到

  1. /**
  2. * 格式化价格
  3. *
  4. * @param argStr 传入价格字符串
  5. * @return
  6. */
  7. public static String getFloatDotStr(String argStr) {
  8. float arg = Float.valueOf(argStr);
  9. DecimalFormat fnum = new DecimalFormat("##0.00");
  10. return fnum.format(arg);
  11. }

2.获取App的版本号Version

  1. // 得到versionCode
  2. public static int getVerCode(Context context) {
  3. int verCode = 0;
  4. try {
  5. verCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
  6. } catch (NameNotFoundException e) {
  7. e.printStackTrace();
  8. }
  9. return verCode;
  10. }

3.获取手机屏幕宽高:

  1. /**
  2. * 屏幕宽高
  3. *
  4. * @param context
  5. * @return 0:width,1:height
  6. */
  7. public static int[] ScreenSize(Context context) {
  8. DisplayMetrics metrics = new DisplayMetrics();
  9. ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
  10. return new int[] { metrics.widthPixels, metrics.heightPixels };
  11. }

4.判断SD卡是否存在:

  1. /**
  2. * 判断sd卡是否存在
  3. *
  4. * @author
  5. * @return
  6. */
  7. public static boolean judgeSDCard() {
  8. String status = Environment.getExternalStorageState();
  9. return status.equals(Environment.MEDIA_MOUNTED);
  10. }

5.验证是否中文:

  1. /***
  2. * 验证是否中文
  3. *
  4. * @param
  5. * @return
  6. */
  7. public static boolean validName(String name) {
  8. String pattern = "[\u4e00-\u9fa5]";
  9. if (isNull(name))
  10. return false;
  11. return name.matches(pattern);
  12. }

6.验证身份证号码:

  1. /**
  2. * 验证身份证号码
  3. *
  4. * @author
  5. * @param idCard
  6. * @return
  7. */
  8. public static boolean validateIdCard(String idCard) {
  9. if (isNull(idCard))
  10. return false;
  11. String pattern = "^[0-9]{17}[0-9|xX]{1}$";
  12. return idCard.matches(pattern);
  13. }

7.验证手机号码:

  1. /**
  2. * 验证手机号码
  3. *
  4. * @author
  5. * @param phone
  6. * @return
  7. */
  8. public static boolean validatePhone(String phone) {
  9. if (isNull(phone))
  10. return false;
  11. String pattern = "^1[3,4,5,6,8]\\d{9}$";
  12. return phone.matches(pattern);
  13. }

8.验证邮编:

  1. /**
  2. * 判断邮编
  3. *
  4. * @param
  5. * @return
  6. */
  7. public static boolean isZipNO(String zipString) {
  8. String str = "^[1-9][0-9]{5}$";
  9. return Pattern.compile(str).matcher(zipString).matches();
  10. }

9.验证银行卡号:

  1. /**
  2. * 验证银行卡号
  3. *
  4. * @param bankCard
  5. * 信用卡是16位,其他的是13-19位
  6. * @return
  7. */
  8. public static boolean validateBankCard(String bankCard) {
  9. if (isNull(bankCard))
  10. return false;
  11. String pattern = "^\\d{13,19}$";
  12. return bankCard.matches(pattern);
  13. }

10.验证邮箱:

  1. /**
  2. * 验证邮箱
  3. *
  4. * @author
  5. * @param email
  6. * @return
  7. */
  8. public static boolean validateEmail(String email) {
  9. if (isNull(email))
  10. return false;
  11. String pattern = "^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$";
  12. return email.matches(pattern);
  13. }

11.将图片设置为圆角图片:

  1. /**
  2. * 设置圆角的图片
  3. *
  4. * @author
  5. * @param bitmap
  6. * 图片
  7. * @param pixels
  8. * 角度
  9. * @return
  10. */
  11. public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
  12. try {
  13. if (bitmap != null) {
  14. Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
  15. Canvas canvas = new Canvas(output);
  16.  
  17. final int color = 0xff424242;
  18. final Paint paint = new Paint();
  19. final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
  20. final RectF rectF = new RectF(rect);
  21. final float roundPx = pixels;
  22.  
  23. paint.setAntiAlias(true);
  24. canvas.drawARGB(0, 0, 0, 0);
  25. paint.setColor(color);
  26. canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
  27.  
  28. paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
  29. canvas.drawBitmap(bitmap, rect, rect, paint);
  30.  
  31. return output;
  32. }
  33. } catch (Exception e) {
  34. }
  35.  
  36. return bitmap;
  37. }
  38.  
  39. /**
  40. * 将图片转换为圆形的
  41. *
  42. * @author
  43. * @param bitmap
  44. * @return
  45. */
  46. public static Bitmap toRoundBitmap(Bitmap bitmap) {
  47. if (bitmap != null) {
  48. bitmap = cutSquareBitmap(bitmap);
  49. return toRoundCorner(bitmap, bitmap.getWidth() / 2);
  50. }
  51. return bitmap;
  52. }

12.判断有无网络链接

  1. // 判断有无网络链接
  2. public static boolean checkNetworkInfo(Context mContext) {
  3. ConnectivityManager conMan = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
  4. State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
  5. State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
  6. if (mobile == State.CONNECTED || mobile == State.CONNECTING)
  7. return true;
  8. if (wifi == State.CONNECTED || wifi == State.CONNECTING)
  9. return true;
  10. return false;
  11. }

13.判断是否连接wifi

  1. /**
  2. * 判断是否连接wifi
  3. *
  4. * @param mContext
  5. * @return 返回true则有wifi
  6. */
  7. private static boolean isWifi(Context mContext) {
  8. ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
  9. NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
  10. if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
  11. return true;
  12. }
  13. return false;
  14. }

14.获取SIM卡存在的状态 

  1. /** 获取SIM卡存在的状态 */
  2. public static String getSIMCardExist(Context context) {
  3. manager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
  4. String state = "";
  5. switch (manager.getSimState()) {
  6. case TelephonyManager.SIM_STATE_READY:
  7. state = "良好";
  8. break;
  9. case TelephonyManager.SIM_STATE_ABSENT:
  10. state = "无SIM卡";
  11. break;
  12. default:
  13. state = "SIM卡被锁定或未知状态";
  14. break;
  15. }
  16. return state;
  17. }

15. bitmap和base64类型互转

  1. // 把bitmap转换成base64
  2. public static String getBase64FromBitmap(Bitmap bitmap, int bitmapQuality) {
  3. ByteArrayOutputStream bStream = new ByteArrayOutputStream();
  4. bitmap.compress(CompressFormat.JPEG, bitmapQuality, bStream);
  5. byte[] bytes = bStream.toByteArray();
  6. return Base64.encode(bytes);
  7. }
  8.  
  9. // 把base64转换成bitmap
  10. public static Bitmap getBitmapFromBase64(String string) {
  11. byte[] bitmapArray = null;
  12. try {
  13. bitmapArray = Base64.decode(string);
  14. } catch (Exception e) {
  15. e.printStackTrace();
  16. }
  17. return BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
  18. }

16. 把图片流转换成byte数组

  1. /**
  2. *
  3. * 方法说明 把图片流转换成byte数组
  4. *
  5. * @author
  6. * @param
  7. * @return
  8. */
  9. public static byte[] getByteFromStream(InputStream inStream) {
  10. byte[] data = new byte[1024];
  11. byte[] buffer = new byte[1024];
  12. int len = -1;
  13. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  14. try {
  15. while ((len = inStream.read(buffer)) != -1) {
  16. outStream.write(buffer, 0, len);
  17. }
  18. data = outStream.toByteArray();
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. } finally {
  22. try {
  23. outStream.close();
  24. inStream.close();
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. return data;
  30. }

17.把字节数组转化成bitmap

  1. /**
  2. *
  3. * 把字节数组转化成bitmap
  4. *
  5. * @author
  6. * @param
  7. * @return
  8. */
  9. public static Bitmap getBitmapFromBytes(byte[] bytes, BitmapFactory.Options opts) {
  10. if (bytes != null)
  11. if (opts != null)
  12. return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts);
  13. else
  14. return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
  15. return null;
  16. }

18.把Stream转换成String

  1. // 把Stream转换成String
  2. public static String convertStreamToString(InputStream is) {
  3. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  4. StringBuilder sb = new StringBuilder();
  5. String line = null;
  6.  
  7. try {
  8. while ((line = reader.readLine()) != null) {
  9. sb.append(line + "/n");
  10. }
  11. } catch (IOException e) {
  12. e.printStackTrace();
  13. } finally {
  14. try {
  15. is.close();
  16. } catch (IOException e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. return sb.toString();
  21. }

19.防止按钮连续点击

  1. /**
  2. * 防止按钮连续点击
  3. */
  4. private static long lastClickTime;
  5.  
  6. public static boolean isFastDoubleClick() {
  7. long time = System.currentTimeMillis();
  8. long timeD = time - lastClickTime;
  9. if (0 < timeD && timeD < 500) {
  10. return true;
  11. }
  12. lastClickTime = time;
  13. return false;
  14. }

20.保存图片到SD卡

  1. /**
  2. * 保存图片
  3. *
  4. * @param photoBitmap
  5. * @param path 保存路径
  6. */
  7. public static void savePhotoToSDCard(Bitmap photoBitmap, String path) {
  8.  
  9. File photoFile = new File(path);
  10. FileOutputStream fileOutputStream = null;
  11. try {
  12. fileOutputStream = new FileOutputStream(photoFile);
  13. if (photoBitmap != null) {
  14. if (photoBitmap.compress(Bitmap.CompressFormat.JPEG, 70, fileOutputStream)) {
  15. fileOutputStream.flush();
  16. }
  17. }
  18. } catch (FileNotFoundException e) {
  19. photoFile.delete();
  20. e.printStackTrace();
  21. } catch (IOException e) {
  22. photoFile.delete();
  23. e.printStackTrace();
  24. } finally {
  25. try {
  26. fileOutputStream.close();
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }

21.将字符串转化为二维码:

  1. private static int QR_WIDTH = 300;
  2. private static int QR_HEIGHT = 300;
  3.  
  4. public static Bitmap createQRImage(String url) {
  5. try {
  6. // 判断URL合法性
  7. if (url == null || "".equals(url) || url.length() < 1) {
  8. return null;
  9. }
  10. Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
  11. hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
  12. // 图像数据转换,使用了矩阵转换
  13. BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT);
  14. int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
  15. // 下面这里按照二维码的算法,逐个生成二维码的图片,
  16. // 两个for循环是图片横列扫描的结果
  17. for (int y = 0; y < QR_HEIGHT; y++) {
  18. for (int x = 0; x < QR_WIDTH; x++) {
  19. if (bitMatrix.get(x, y)) {
  20. pixels[y * QR_WIDTH + x] = 0xff000000;
  21. } else {
  22. pixels[y * QR_WIDTH + x] = 0xffffffff;
  23. }
  24. }
  25. }
  26. // 生成二维码图片的格式,使用ARGB_8888
  27. Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);
  28. bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
  29. return bitmap;
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33. return null;
  34. }

22.得到自定义进度框:

  1. /**
  2. * 得到自定义的progressDialog
  3. *
  4. * @param context
  5. * @param msg
  6. * @param bCancel
  7. * 按返回键是否取消
  8. * @return
  9. */
  10. public static Dialog createLoadingDialog(Context context, String msg, boolean bCancel) {
  11.  
  12. LayoutInflater inflater = LayoutInflater.from(context);
  13. View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载view
  14. LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
  15. // main.xml中的ImageView
  16. ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);
  17. TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
  18. // 加载动画
  19. Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(context, R.anim.loading_animation);
  20. // 使用ImageView显示动画
  21. spaceshipImage.startAnimation(hyperspaceJumpAnimation);
  22. tipTextView.setText(msg);// 设置加载信息
  23.  
  24. Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog
  25.  
  26. loadingDialog.setCancelable(bCancel);// 不可以用“返回键”取消
  27. loadingDialog.setCanceledOnTouchOutside(false);
  28. loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
  29. return loadingDialog;
  30.  
  31. }
  32.  

Android 开发实用方法大全的更多相关文章

  1. ANDROID开发实用小工具

    分享一些 Android开发中的实用小工具,你有发现好工具吗? 来这里分享一下呗 一.find bugs 静态检查工具 http://findbugs.sourceforge.net/ FindBug ...

  2. Android 开发工具方法整理

    1. 获取当前手机系统语言 Locale.getDefault().getLanguage(); 2. 获取当前手机系统版本号 android.os.Build.VERSION.RELEASE; 3. ...

  3. Android开发-- findViewById()方法得到空指针

    如果想通过调用findViewById()方法获取到相应的控件,必须要求当前Activity的layout通过setContentView. 如果你通过其他方法添加了一个layout,如需获取这个la ...

  4. Android零基础入门第17节:Android开发第一个控件,TextView属性和方法大全

    原文:Android零基础入门第17节:Android开发第一个控件,TextView属性和方法大全 前面简单学习了一些Android UI的一些基础知识,那么接下来我们一起来详细学习Android的 ...

  5. Android开发中,那些让您觉得相见恨晚的方法、类或接口

    Android开发中,那些让你觉得相见恨晚的方法.类或接口本篇文章内容提取自知乎Android开发中,有哪些让你觉得相见恨晚的方法.类或接口?,其实有一部是JAVA的,但是在android开发中也算常 ...

  6. Android开发工具常用快捷键大全

    Android开发中常用的开发工具有android studio和eclipse两种,下面小编整理了一些这两种开发工具中常用的快捷键,使用这些快捷键,你的android编程将事半功倍. android ...

  7. Android零基础入门第22节:ImageView的属性和方法大全

    原文:Android零基础入门第22节:ImageView的属性和方法大全 通过前面几期的学习,TextView控件及其子控件基本学习完成,可以在Android屏幕上显示一些文字或者按钮,那么从本期开 ...

  8. [转]Android通过NDK调用JNI,使用opencv做本地c++代码开发配置方法

    原文地址:http://blog.csdn.net/watkinsong/article/details/9849973 有一种方式不需要自己配置所有的Sun JDK, Android SDK以及ND ...

  9. 在android开发中使用multdex的方法-IT蓝豹为你整理

    Android系统在安装应用时,往往需要优化Dex,而由于处理工具DexOpt对id数目的限制,导致其处理的数目不能超过65536个,因此在Android开发中,需要使用到MultiDex来解决这个问 ...

随机推荐

  1. Android—基于OpenCV+Android实现人脸检测

    导读 OpenCV 是一个开源的跨平台计算机视觉库, 采C++语言编写,实现了图像处理和计算机视觉方面的很多通用算法,同时也提供对Python,Java,Android等的支持,这里利用Android ...

  2. nginx开机自启动

    配置步骤:1 . vi /etc/init.d/nginx2. chkconfig --level nginx 2345 on --重点 --------------------以下为nginx配置文 ...

  3. [STL] 遍历删除两个vector中交集

    #include <vector> #include <string> #include <algorithm> using namespace std; int ...

  4. BZOJ3522 POI2014HOT-Hotels(树形dp)

    分两种情况.三点两两lca相同:在三点的lca处对其统计即可,显然其离lca距离应相同:某点在另两点lca的子树外部:对每个点统计出与其距离x的点有多少个即可. 可以长链剖分做到线性,当然不会. #i ...

  5. [bzoj1056] [HAOI2008]排名系统

    Description 排名系统通常要应付三种请求:上传一条新的得分记录.查询某个玩家的当前排名以及返回某个区段内的排名记录.当某个玩家上传自己最新的得分记录时,他原有的得分记录会被删除.为了减轻服务 ...

  6. [bzoj5321] [Jxoi2017]加法

    Description 可怜有一个长度为 n 的正整数序列 A,但是她觉得 A 中的数字太小了,这让她很不开心. 于是她选择了 m 个区间 [li, ri] 和两个正整数 a, k.她打算从这 m 个 ...

  7. 洛谷P1546 最短网络 Agri-Net

    P1546 最短网络 Agri-Net 526通过 959提交 题目提供者JOHNKRAM 标签图论贪心USACO 难度普及/提高- 提交该题 讨论 题解 记录 最新讨论 50分C++代码,求解 请指 ...

  8. Dilworth定理证明

    命题:偏序集能划分成的最少的全序集的个数与最大反链的元素个数相等. (离散数学结构第六版课本P245:把一个偏序集划分成具有全序的子集所需要的最少子集个数与元素在偏序下都是不可比的最大集合的基数之间有 ...

  9. angular js自定义service的简单示例

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  10. PHP报错Cannot adopt OID in UCD-SNMP-MIB、 LM-SENSORS-MIB

    Cannot adopt OID in UCD-SNMP-MIB: Cannot adopt OID in LM-SENSORS-MIB: lmTempSensorsValue 运行PHP遇到这些错误 ...