文章目录
1.SimpleDateFormat格式
- yyyy:年
- MM:月
- dd:日
- E:星期几
- D:一年中的第几天
- F:一月中的第几个星期(会把这个月总共过的天数除以7)
- w:一年中的第几个星期
- W:一月中的第几星期(会根据实际情况来算)
- hh:1~12小时制(1-12)
- HH:24小时制(0-23)
- a:上下午标识
- mm:分
- ss:秒
- SS:毫秒
- k:表示一天24小时制(1-24)。
- K:表示一天12小时制(0-11)。
- z:表示时区
- L:月 L:1 LL:01 LLL:Jan LLLL:January LLLLL:J
new SimpleDateFormat(pattern).format(new Date(long date)))
2.带参数的string
- 命名空间:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
- %n$ms:表示输出的是字符串,n代表是第几个参数,设置m的值可以在输出之前放置空格;
- %n$md:输出的是整数,n代表是第几个参数,设置m的值可以在输出之前放置空格;
- %n$mf:输出的是浮点数,n代表是第几个参数,设置m的值可以控制小数位数,如m=2.2时,输出格式为00.00;
- 例子:
<string name="details_size">File size: <xliff:g id="size">%1$d</xliff:g><xliff:g id="danwei">%2$s</xliff:g></string>
%
后面跟的n
表示第几个参数,$
后面跟的m
是精度,s
是类型
3.TextView省略号
- maxlinex:
android:maxLines="1"
- ellipsize:
android:ellipsize="end"
4.删除本地图片
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver mContentResolver = getContentResolver();
String where = MediaStore.Images.Media.DATA + "='" + getCurrentPicturePath() + "'";
mContentResolver.delete(uri,where,null);
- getCurrentPicturePath()获取图片的path
5.分享本地图片到蓝牙和短信
Intent share_intent = new Intent();
ArrayList<Uri> imageUris = new ArrayList<Uri>();
Uri imageContentUri = getImageContentUri(new File(getCurrentPicturePath()));
imageUris.add(imageContentUri);
share_intent.setPackage(packagename);
share_intent.setAction(Intent.ACTION_SEND_MULTIPLE);
share_intent.setType("image/*");
share_intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
share_intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
startActivity(share_intent);
- packagename:指定蓝牙或者短信的包名
- getCurrentPicturePath():获取图片的path
- getImageContentUri:获取图片的content uri
public Uri getImageContentUri(File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=? ",
new String[]{filePath}, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (imageFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}
直接Uri.fromFile(new File(path))
会出现一个异常FileUriExposedException
android.os.FileUriExposedException: file:///storage/emulated/0/Picts2/1timg.jpg exposed beyond app through ClipData.Item.getUri()
Uri不允许直接暴露。
6.Edittext
- 不设置inputType会导致超过后清空
- textCursorDrawable 引用默认颜色为colorAccent
自定义drawable shape solide color
7.String的replace 和replaceAll
- replace是替换指定内容,如果没有就返回本身,如果为空则返回替换内容。
- 要写成
str = str.replace("%", "÷100");
注意不要少了赋值过程,否则str
还是str
自己 - replaceAll是正则表达式匹配
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
8.时间
Calendar calendar = Calendar.getInstance();
timehour.setText(calendar.get(Calendar.HOUR_OF_DAY)+"");
timeminute.setText(calendar.get(Calendar.MINUTE)+"");
timeampm.setText(calendar.get(Calendar.AM_PM)+"");
9.Dialog对话框
- 自定义全屏dialog
自定义子类继承Dialog,重载show方法
@Override
public void show() {
super.show();
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.gravity = Gravity.CENTER;
layoutParams.y = 12; //垂直偏移量
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
layoutParams.horizontalMargin = 0; //水平margin
getWindow().getDecorView().setPadding(0, 0, 0, 0); //decorview 无padding
getWindow().getDecorView().setBackgroundColor(Color.WHITE); //背景色必须设置
getWindow().getDecorView().setMinimumWidth(getContext().getResources().getDisplayMetrics().widthPixels); //decorviewshe设置为全屏宽度
getWindow().setAttributes(layoutParams);
}
10. 检测系统时间是不是24小时格式
DateFormat.java 来自framework
boolean is24hour = DateFormat.is24HourFormat(getContext());
11. 检测当前打开的app是否是指定的app 或activity
String MY_TEST_NAME = "xxxx"
public boolean checkMyActivity() {
boolean isAppRunning = false;
try {
if (ActivityManager.getService()
.getTasks(3, 0)
.get(0)
.topActivity
.getClassName() //匹配类名
//.getPackageName() 匹配包名
.equals(MY_TEST_NAME )) {
isAppRunning = true;
}
} catch (RemoteException e) {
e.printStackTrace();
}
return isAppRunning;
}
12. 无触摸屏,获取Listview子view焦点
listView.setItemsCanFocus(true);
listView.requestFocus();
listView.setSelection(0);
listView.getChildAt(0).requestFocus();
listView.requestChildFocus(listView.getChildAt(0), listView.getChildAt(0));
listView.getFocusedChild();
还要保证Item的layout设置的background要是一个selector,这样聚焦的时候才看得到获取焦点的效果。
13. 沉浸式全屏,隐藏状态栏,导航栏
View contentview = LayoutInflater.from(this).inflate(R.layout.bummer, null);
contentview.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_IMMERSIVE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
setContentView(contentview);
14. 设置设备唯一序列号
CUSTOM_LK_USB_UNIQUE_SERIAL=yes
15.检测系统是否第一次启动
private boolean isFirstBootOrUpgrade() {
return mPackageManagerService.isFirstBoot() || mPackageManagerService.isUpgrade();
}
16.删除特殊字符名称的文件
ls -i1 //查看文件的inode 号
find . -inum 63181495 -delete //删除对应inode号的文件