[置顶] 开发中常用代码片段汇总

1.如何求两个整数的百分比

int currentindex = 55;
int totalcount = 66;
NumberFormat nf = NumberFormat.getPercentInstance();
final String persent = nf.format(((float)currentindex/(float)totalcount));


2.从一个链接中获取域名

String url = "http://blog.csdn.net/zuolongsnail";
Pattern p = Pattern.compile(
		"(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)",
		Pattern.CASE_INSENSITIVE);
Matcher matcher = p.matcher(url);
matcher.find();
System.out.println(matcher.group());


3.显示年月日时分秒星期

Date date = new Date();
SimpleDateFormat dateformat = new SimpleDateFormat(
		"yyyy年MM月dd日 HH时mm分ss秒 E");
String dateStr = dateformat.format(date);
System.out.println(dateStr);

4.显示年月日时分秒毫秒

Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒SSS");
String dateStr = sdf.format(date);
System.out.println(dateStr);


5.发送多条短信

PendingIntent sentPI = PendingIntent.getBroadcast(context,
		System.identityHashCode(intent), intent, 0);
SmsManager smsManager = SmsManager.getDefault();
// 超过70字分段发送
if (text.length() > 70) {
	ArrayList<PendingIntent> sentPIs = new ArrayList<PendingIntent>();
	sentPIs.add(sentPI);
	smsManager.sendMultipartTextMessage(destAdress, null,
			smsManager.divideMessage(text), sentPIs, null);
} else {
	smsManager.sendTextMessage(destAdress, null, text, sentPI, null);
}


6.屏蔽多点触摸

android:splitMotionEvents="false"






你可能感兴趣的:(java,android,技巧,代码段)