Android 常見方法

來源:互聯網
上載者:User

標籤: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 常見方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.