zoukankan      html  css  js  c++  java
  • Android 常见方法

    /***
         * 判断手机号是否合法
         * @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);
        }
  • 相关阅读:
    洛谷 P3613 【深基15.例2】寄包柜
    洛谷 P1478 陶陶摘苹果(升级版)
    P1090 [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G
    c++优先队列(priority_queue)用法详解
    洛谷 P3817 小A的糖果
    洛谷 P1208 [USACO1.3]混合牛奶 Mixing Milk
    洛谷 P1449 后缀表达式
    洛谷 P1106 删数问题
    在Linux中使用tomcat部署项目
    jar在linux上运行脚本 #start #stop #restart
  • 原文地址:https://www.cnblogs.com/xunzhi/p/4155061.html
Copyright © 2011-2022 走看看