您的位置:首页 > 移动开发 > Android开发

Android 开发小知识(持续更新)

2016-06-07 17:33 483 查看
一、显示一个随系统设置变化的时间(12小时制度下 中文 上午表示为上午、英文环境上午为 AM)

/**
* 获取时间字串,时间随系统时间显示制和时区改变
*
* @param context
* @return
*/
public String getSystemTimeStr(Context context) {
long now = System.currentTimeMillis();
String timeStr = "";
//判断是否是24小时制
SimpleDateFormat timeformat = null;
if (DateFormat.is24HourFormat(context)) {
timeformat = new SimpleDateFormat("HH:mm");
} else {
timeformat = new SimpleDateFormat("h:mm a");
}
//设置随时区变化
timeformat.setTimeZone(TimeZone.getDefault());
timeStr = timeformat.format(now);
return timeStr;
}
时间模式字符串用来指定时间格式。在此模式中,所有的ASCII字母被保留为模式字母,定义如下:

字母描述示例
G纪元标记AD
y四位年份2001
M月份July or 07
d一个月的日期10
h A.M./P.M. (1~12)格式小时12
H一天中的小时 (0~23)22
m分钟数30
s秒数55
S微妙数234
E星期几Tuesday
D一年中的日子360
F一个月中第几周的周几2 (second Wed. in July)
w一年中第几周40
W一个月中第几周1
aA.M./P.M. 标记PM
k一天中的小时(1~24)24
K A.M./P.M. (0~11)格式小时10
z时区Eastern Standard Time
'文字定界符Delimiter
"单引号`
二、 扩大android 中View 的点击区域

1 设置padding 值

2 扩大响应区域(见下)

/**
* 扩大View的触摸和点击响应范围,最大不超过其父View范围
*
* @param view
* @param top
* @param bottom
* @param left
* @param right
*/
public static void expandViewTouchDelegate(final View view, final int top,
final int bottom, final int left, final int right) {

((View) view.getParent()).post(new Runnable() {
@Override
public void run() {
Rect bounds = new Rect();
view.setEnabled(true);
view.getHitRect(bounds);

bounds.top -= top;
bounds.bottom += bottom;
bounds.left -= left;
bounds.right += right;

TouchDelegate touchDelegate = new TouchDelegate(bounds, view);

if (View.class.isInstance(view.getParent())) {
((View) view.getParent()).setTouchDelegate(touchDelegate);
}
}
});
}
恢复点击区域

/**
* 还原View的触摸和点击响应范围,最小不小于View自身范围
*
* @param view
*/
public static void restoreViewTouchDelegate(final View view) {

((View) view.getParent()).post(new Runnable() {
@Override
public void run() {
Rect bounds = new Rect();
bounds.setEmpty();
TouchDelegate touchDelegate = new TouchDelegate(bounds, view);

if (View.class.isInstance(view.getParent())) {
((View) view.getParent()).setTouchDelegate(touchDelegate);
}
}
});
}

原理就是在view的onTouchEvent里面



三、获取CU

/**
* 高通芯片 通过反射获取CU,PTS,PTH,BSN参数
*
* @return
*/
public String getCU() {
try {
Class<?> systemPropertiesClass = Class.forName("android.os.SystemProperties");
Method getMethod = systemPropertiesClass.getMethod("get", String.class);
Object object = new Object();
Object obj = getMethod.invoke(object, "ro.tct.curef");
return (obj == null ? "" : (String) obj);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android