边开发边记录一些便利的小知识,不定时更新(如有错误,请不吝指教,谢谢):
1、String 里面的占位符的使用,网上有很多
// String 文件
<string name="hint">字符串%1$s,字符串%2$s,整数%3$d,浮点型两位小数:%4$.2f,浮点型三位小数:%5$.3f</string>
代码:
String string = getResources().getString(R.string.hint);
String format = String.format(string, "哈哈","hehe",4,3,3.1);
textView.setText(format);
输出:
字符串哈哈,字符串hehe,整数4,浮点型两位小数:3.00,浮点型三位小数:3.100
2、防止VIew上信息被其他软件截屏或系统截图泄漏信息
setContentView(...);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
3、快速看方法调用栈顺序
RuntimeException here = new RuntimeException("bolex");
here.fillInStackTrace();
Log.w("myTag", "Called: " + this, here);
4、全局查看项目无用资源
项目右键,Analyze ==> run inspaction by Name ==> 弹出对话框输入Unused Resources ==> 选择要查找的module,点击OK即可。
5、去重
ArrayList newList=new ArrayList<>(new TreeSet(strList));
去重 假设 strList里面有三个值 分别为:str1 str2 str1
我们通过上面的代码 newList等于 str1 str2
6、常用的SpannableString
start开始的位置、end结束的位置,记住:含头不含尾
SpannableString spannableString = new SpannableString(string);
spannableString.setSpan(Object,start,end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
Object 为你需要的效果,有以下几种:
1、new ForegroundColorSpan(colcor); 前景色,一般是指字体颜色
2、new BackgroundColorSpan(color); 背景色,一般指background
3、new TextClickSpan(); 可触发点击事件
TextClickSpan clickableSpan = new TextClickSpan(link);
spannableString.setSpan(clickableSpan,start,end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
tvTest.setMovementMethod(LinkMovementMethod.getInstance());
tvTest.setText(spannableString);
class TextClickSpan extends ClickableSpan {
private String content;
public MyClickableSpan(String content) {
this.content = content;
}
@Override
public void updateDrawState(TextPaint ds) {
// 更新一些显示状态
}
@Override
public void onClick(View widget) {
// 点击事件
}
}
4、new UnderlineSpan(); 下划线
5、new StrikethroughSpan(); 中划线
6、new AbsoluteSizeSpan(20, true);绝对大小(文本字体)
7、new RelativeSizeSpan(2.5f);相对大小(文本字体),在原来的基本上放大多少倍
8、new SuperscriptSpan() 上标
9、new SubscriptSpan() 下标
10、new StyleSpan(style) 设置字体样式正常,粗体,斜体,粗斜体
style = android.graphics.Typeface.NORMAL //正常
style = android.graphics.Typeface.BOLD //粗体
style = android.graphics.Typeface.ITALIC //斜体
style = android.graphics.Typeface.BOLD_ITALIC //粗斜体
11、new ImageSpan(drawable); 设置文本图片
Drawable drawable = getResources().getDrawable(R.mipmap.a9c);
drawable.setBounds(0, 0, 42, 42);
ImageSpan imageSpan = new ImageSpan(drawable);
7、获取时间
import java.text.SimpleDateFormat;
// 第一种
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date curDate = new Date(System.currentTimeMillis());//获取当前时间
String str = formatter.format(curDate);
// 第二种
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String date = sDateFormat.format(new java.util.Date());
// 当前是12小时制还是24小时制
ContentResolver cv = this.getContentResolver();
String strTimeFormat = android.provider.Settings.System.getString(cv,android.provider.Settings.System.TIME_12_24);
if(strTimeFormat.equals("24")) {
Log.i("activity","24");
}
// 第四种 Calendar获取
Calendar c = Calendar.getInstance();
取得系统日期: year = c.get(Calendar.YEAR)
month = c.grt(Calendar.MONTH)
day = c.get(Calendar.DAY_OF_MONTH)
取得系统时间: hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE)
8、String 的 split 方法,分割 “.” 无效,需添加双斜杠 \ 转义分割
当分割"." 的时候,分割得到的数组的数量是0,但是字符串contain(".")返回的是true,
解决方法:分割的时候,需要添加双斜杠 \\ 进行转义,应该为 split("\\.")