1.Gson 写一个类,包含json串中的变量
public class GsonUtils {
public static <T> List<T> readJsonArray(JSONArray array,Class<T> entityType){
Gson gson=new Gson();
List<T> list=new ArrayList<>();
for(int i=0;i<array.length();i++){
try {
T t=gson.fromJson(array.getJSONObject(i).toString(),entityType);
list.add(t);
} catch (JSONException e) {
e.printStackTrace();
}
}
return list;
}
}
2.Toast
public class ToastUtils {
public static void showToast(String content){
Toast.makeText(BaseApplication.getContext(), content, Toast.LENGTH_SHORT).show();
}
}
3.dp转px,px转dp
public class DensityUtil {
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}