美文网首页
Android 常见方法

Android 常见方法

作者: 明_逊志 | 来源:发表于2016-08-16 10:35 被阅读0次

/***

* 判断手机号是否合法

* @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 常见方法

      本文链接:https://www.haomeiwen.com/subject/azlpsttx.html