【Android】一些好用的方法(持续更新)
2020-03-26 本文已影响0人
Blue_Well
-
打印的方式
声明了一个public属性后,tag只需要添加相应的名字即可,省去了每次都要输入。
public static final String TAG = "MainActivity";
Log.d(TAG, tempData);
- 删除列表中重复的数据
HashSet<String> hash_mac = new HashSet<>(deviceMac_list);
deviceMac_list.clear();
deviceMac_list.addAll(hash_mac);
- 判断列表是否有重复的数据
private ArrayList<String> deviceMacList = new ArrayList<>();
private ArrayList<String> deviceNameList = new ArrayList<>();
for(int i = 0; i < deviceMac_list.size(); i++){
if (deviceMac_list.get(i).equals(device_mac)){
return;
}
}
-
打印数组的方法
这里的数组可以是byte[]或其他类型。
Log.d(TAG, Arrays.toString(rxData));
- 各种数字类型转换成字符串型
float a = 23.4f;
String s = Float.toString(a) ;
int a = 10;
String s = Integer.toString(i);
double a = 10.1234;
String s = Double.toString(i);
- 字符串型转换成各种数字类型
String s = "169";
byte b = Byte.parseByte(s);
short t = Short.parseShort(s);
int i = Integer.parseInt(s);
long l = Long.parseLong(s);
Float f = Float.parseFloat(s);
Double d = Double.parseDouble(s);