標籤:android style blog io ar color sp for on
/*** * 判斷手機號是否合法 * @see 十一位元字,1開頭 * @param mobiles * @return boolean */ public static boolean isMobile(String mobiles) { if (null==mobiles) { return false; } if (mobiles.length()!=11) { return false; } for (int i = mobiles.length();--i>=0;){ if (!Character.isDigit(mobiles.charAt(i))){ return false; } } if (!mobiles.startsWith("1")) { return false; } return true; }
/** * 根據手機的解析度從 dp 的單位 轉成為 px(像素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 根據手機的解析度從 px(像素) 的單位 轉成為 dp */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); }
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); public static SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm"); public static SimpleDateFormat sdf2 = new SimpleDateFormat("MM-dd"); public static SimpleDateFormat sdf3 = new SimpleDateFormat("yy-MM-dd"); public static SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy"); public static SimpleDateFormat sdf5 = new SimpleDateFormat("MM-dd HH:mm"); public static SimpleDateFormat sdf6 = new SimpleDateFormat("yyyyMMddHHmmss"); /** * @param string * @return * 私信列表時間顯示說明: * 當天:顯示小時:分鐘(hh:mm)。 * 非當天:顯示月-日(mm-dd) * 非當年:顯示年-月-日(yy-mm-dd) */ public static String getLetterListDate(long time) { Date nowDate = new Date(); Date date = new Date(time); if (!sdf4.format(nowDate).equals(sdf4.format(date))) { return sdf3.format(date); } else if (sdf3.format(nowDate).equals(sdf3.format(date))) { return sdf1.format(date); } else { return sdf2.format(date); } } /** * @param string * @return * 私信列表時間顯示說明: * 當天:顯示小時:分鐘(hh:mm)。 * 非當天:顯示月-日(mm-dd) * 非當年:顯示年-月-日(yy-mm-dd) */ public static String getLetterListDatesimple(long time) { Date nowDate = new Date(); Date date = new Date(time); if (!sdf4.format(nowDate).equals(sdf4.format(date))) { return sdf3.format(date); } else if (sdf3.format(nowDate).equals(sdf3.format(date))) { return sdf1.format(date); } else { return sdf2.format(date); } } /** * @param string * @return * 私信聊天視窗時間顯示說明: * 當天:顯示小時:分鐘(hh:mm)。 * 非當天:顯示月-日 小時:分鐘(mm-dd hh:mm) * 非當年:顯示年-月-日 小時:分鐘(yy-mm-dd hh:mm) */ public static String getLetterDate(long time) { Date nowDate = new Date(); Date date = new Date(time); if (!sdf4.format(nowDate).equals(sdf4.format(date))) { return sdf.format(date); } else if (sdf3.format(nowDate).equals(sdf3.format(date))) { return sdf1.format(date); } else { return sdf5.format(date); } }
/** * 計算text的字數,一個漢字=兩個英文字母,一個中文標點=兩個英文標點 注意:該函數的不適用於對單個字元進行計算,因為單個字元四捨五入後都是1 * * @param c * @return */ public static long calculateLength(CharSequence c) { double len = 0; for (int i = 0; i < c.length(); i++) { int tmp = (int) c.charAt(i); if (tmp > 0 && tmp < 127) { len += 0.5; } else { len++; } } return Math.round(len); }
public static void openActivityAnim(Activity activity){ activity.overridePendingTransition(R.anim.tran_next_in, R.anim.tran_next_out); } public static void finishActivityAnim(Activity activity){ activity.overridePendingTransition(R.anim.left_in,R.anim.right_out); }
Android 常見方法