一、描述
在项目中经常会遇到一些问题,网上搜到的一些比较好的解决方案,在这里分享出来。
二、方法集合
1 监听编辑框的回车事件
//对编辑框设置一个监听
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//当actionId == XX_SEND 或者 XX_DONE时都触发
//或者event.getKeyCode == ENTER 且 event.getAction == ACTION_DOWN时也触发
//注意,这是一定要判断event != null。因为在某些输入法上会返回null。
if (actionId == EditorInfo.IME_ACTION_SEND
|| actionId == EditorInfo.IME_ACTION_DONE
|| (event != null && KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN == event.getAction())) {
//处理回车事件...
}
return true;
}
});
2 通知媒体库,有的通知方法在4.4前可以用,在之后就无效了,下面给出一个能够两个版本前后都解决的方法,(单个文件通知)
传入文件全路径,4.4之后的方法,之前也可以用
MediaScannerConnection.scanFile(activity,paths, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
//通知完成,处理自己的事情
}
});
3 对图片的完整性进行检查,项目中有遇到服务端返回无效图片,也被我下载下来了,所以在下载后需要对图片进行完整性检查。
//图片路径
String filePath = path;
BitmapFactory.Options options = null;
if (options == null) options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
if (options.mCancel || options.outWidth == -1
|| options.outHeight == -1) {
//表示图片已损毁,
}
4 Snackbar用到的时候,虚拟键会挡住?
//用findViewById获取的这个view不会被挡住
View rootView = activity.findViewById(android.R.id.content)
Snackbar.make(rootView, "创建备忘成功,可以对新备忘进行打标签", Snackbar.LENGTH_SHORT).show();
5 获取软键盘的高度
SoftKeyBoardListener.getSoftKeyboardHeight(root, new SoftKeyBoardListener.OnGetSoftHeightListener() {
@Override
public void onShowed(int height) {
//当软键盘弹出,返回软键盘高度
}
@Override
public void onHide() {
//当软键盘关闭
}
});
public class SoftKeyBoardListener {
/**
* 获取软键盘的高度 * *
*
* @param rootView *
* @param listener
*/
public static void getSoftKeyboardHeight(final View rootView, final OnGetSoftHeightListener listener) {
final ViewTreeObserver.OnGlobalLayoutListener layoutListener
= new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
final Rect rect = new Rect();
rootView.getWindowVisibleDisplayFrame(rect);
final int screenHeight = rootView.getRootView().getHeight();
final int heightDifference = screenHeight - rect.bottom;
//设置一个阀值来判断软键盘是否弹出
boolean visible = heightDifference > screenHeight / 3;
if (visible) {
if (listener != null) {
listener.onShowed(heightDifference);
}
} else {
if (listener != null) {
listener.onHide();
}
}
}
};
rootView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
}
public interface OnGetSoftHeightListener {
void onShowed(int height);
void onHide();
}
}
6 高亮文本指定内容
/**
*描述:返回指定高亮的文本,返回的直接用TextView.setText就ok
* content : 内容
* key : 需要高亮的文本
* color : 高亮的颜色
*邮箱:344176791@qq.com
*日期:2017/9/24 下午4:39
*/
public static SpannableStringBuilder getSpannableString(String content, String key, int color) {
SpannableStringBuilder s = new SpannableStringBuilder(content);
Pattern p = Pattern.compile(key);
Matcher m = p.matcher(s);
while (m.find()) {
int start = m.start();
int end = m.end();
s.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return s;
}
7 保存InputStream数据到本地
public static void saveFile(String path, InputStream inputStream, onDownProgressListener onDownProgressListener) {
File file = new File(path);
if (!file.exists()) {
file.mkdir();
}
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(new File(file.getPath()));
int len = 0;
int sum = 0;
byte[] buff = new byte[2048];
while ((len = (inputStream.read(buff))) != -1) {
outputStream.write(buff, 0, len);
sum += len;
}
onDownProgressListener.onSuccess();
} catch (FileNotFoundException e) {
onDownProgressListener.onFail(e.getMessage());
} catch (IOException e) {
onDownProgressListener.onFail(e.getMessage());
}
}
8 Android6.0系统MAC地址获取的方法
/**
* 获取手机的MAC地址
* @return
*/
public String getMac(){
String str="";
String macSerial="";
try {
Process pp = Runtime.getRuntime().exec(
"cat /sys/class/net/wlan0/address ");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (; null != str;) {
str = input.readLine();
if (str != null) {
macSerial = str.trim();// 去空格
break;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
if (macSerial == null || "".equals(macSerial)) {
try {
return loadFileAsString("/sys/class/net/eth0/address")
.toUpperCase().substring(0, 17);
} catch (Exception e) {
e.printStackTrace();
}
}
return macSerial;
}
public static String loadFileAsString(String fileName) throws Exception {
FileReader reader = new FileReader(fileName);
String text = loadReaderAsString(reader);
reader.close();
return text;
}
public static String loadReaderAsString(Reader reader) throws Exception {
StringBuilder builder = new StringBuilder();
char[] buffer = new char[4096];
int readLength = reader.read(buffer);
while (readLength >= 0) {
builder.append(buffer, 0, readLength);
readLength = reader.read(buffer);
}
return builder.toString();
}
三、总结
如果有类似的方法或技术点,也可以留言分享,谢谢!
欢迎关注我的微信公众号,分享更多技术文章。