工欲善其事必先利其器,希望下面的工具类能对你有所帮助,目前比较少,会持续更新。
1.键盘工具类 KeyBoardUtils
public class KeyBoardUtils {
/**
* 自动打开软键盘
*
* @auther css
* created at 2016/4/23 21:58
*/
public static void openKeyboard(Handler mHandler, int s, final Context context) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
}, s);
}
/**
* 关闭软键盘
*
* @param mContext 上下文
*/
public static void closeKeybord(Context mContext) {
InputMethodManager imm = (InputMethodManager) mContext
.getSystemService(Context.INPUT_METHOD_SERVICE);
// imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
imm.hideSoftInputFromWindow(null, 0);
}
}
2.屏幕相关工具类 NetUtils
public class ScreenUtils
{
private ScreenUtils()
{
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 获得屏幕高度
*
* @param context
* @return
*/
public static int getScreenWidth(Context context)
{
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}
/**
* 获得屏幕宽度
*
* @param context
* @return
*/
public static int getScreenHeight(Context context)
{
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.heightPixels;
}
/**
* 获得状态栏的高度
*
* @param context
* @return
*/
public static int getStatusHeight(Context context)
{
int statusHeight = -1;
try
{
Class> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
int height = Integer.parseInt(clazz.getField("status_bar_height")
.get(object).toString());
statusHeight = context.getResources().getDimensionPixelSize(height);
} catch (Exception e)
{
e.printStackTrace();
}
return statusHeight;
}
/**
* 获取当前屏幕截图,包含状态栏
*
* @param activity
* @return
*/
public static Bitmap snapShotWithStatusBar(Activity activity)
{
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
int width = getScreenWidth(activity);
int height = getScreenHeight(activity);
Bitmap bp = null;
bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
view.destroyDrawingCache();
return bp;
}
/**
* 获取当前屏幕截图,不包含状态栏
*
* @param activity
* @return
*/
public static Bitmap snapShotWithoutStatusBar(Activity activity)
{
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
int width = getScreenWidth(activity);
int height = getScreenHeight(activity);
Bitmap bp = null;
bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
- statusBarHeight);
view.destroyDrawingCache();
return bp;
}
}
3.网络相关工具类 NetUtils
/**
* 跟网络相关的工具类
* Created by css on 2016/9/28.
*/
public class NetUtils {
private NetUtils() {
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
public static ConnectivityManager connectivityManager;
public static NetUtils instance;
public static NetUtils getInstance(Context context) {
if (null == instance) {
instance = new NetUtils(context);
}
return instance;
}
public NetUtils(Context context) {
connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
}
/**
* 判断网络是否连接
*
* @param context
* @return
*/
public static boolean isConnected(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (null != connectivity) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (null != info && info.isConnected()) {
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
}
/**
* 判断是否是wifi连接
*/
public static boolean isWifi(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null)
return false;
return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
}
/**
* 判断网络类型
*
* @return
*/
public static String getNetWorkType() {
NetworkInfo mNetworkInfo = connectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
if (ConnectivityManager.TYPE_WIFI == mNetworkInfo.getType())
return "WIFI";
if (ConnectivityManager.TYPE_MOBILE == mNetworkInfo.getType()) {
switch (mNetworkInfo.getSubtype()) {
case TelephonyManager.NETWORK_TYPE_HSUPA:
return "HSUPA";
case TelephonyManager.NETWORK_TYPE_UMTS:
return "UMTS";
case TelephonyManager.NETWORK_TYPE_GPRS:
return "GPRS";
case TelephonyManager.NETWORK_TYPE_EDGE:
return "EDGE";
case TelephonyManager.NETWORK_TYPE_CDMA:
return "CDMA";
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return "EVDO_0";
case TelephonyManager.NETWORK_TYPE_HSPA:
return "HSPA";
case TelephonyManager.NETWORK_TYPE_HSDPA:
return "HSDPA";
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return "EVDO_A";
default:
return "unknown";
}
}
}
return "unknown";
}
/**
* 打开网络设置界面
*/
public static void openSetting(Activity activity) {
Intent intent;
if (android.os.Build.VERSION.SDK_INT > 10) {
intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
} else {
intent = new Intent();
ComponentName component = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
intent.setComponent(component);
intent.setAction("android.intent.action.VIEW");
}
activity.startActivity(intent);
}
}
4. 按钮连击判断工具类 ClickUtil
public class ClickUtil {
private static long lastClickTime;
public static boolean isFastDoubleClick() {
long time = System.currentTimeMillis();
long timeD = time - lastClickTime;
if (0 < timeD && timeD < 1000) {
return true;
}
lastClickTime = time;
return false;
}
}
5. Java指定保留小数位数 DecimalUtils
public class DecimalUtils {
/**
* 按四舍五入保留指定小数位数,位数不够用0补充
* @param o 格式化前的小数
* @param newScale 保留小数位数
* @return 格式化后的小数
*/
public static String formatDecimalWithZero(Object o, int newScale) {
return String.format("%." + newScale + "f", o);
}
/**
* 按四舍五入保留指定小数位数,位数不够用0补充
* @param d 格式化前的小数
* @param newScale 保留小数位数
* @return 格式化后的小数
*/
public static String formatDecimalWithZero(double d, int newScale) {
String pattern = "0.";
for (int i = 0; i < newScale; i++) {
pattern += "0";
}
DecimalFormat df = new DecimalFormat(pattern);
return df.format(d);
}
/**
* 按四舍五入保留指定小数位数,位数不够用0补充
* @param f 格式化前的小数
* @param newScale 保留小数位数
* @return 格式化后的小数
*/
public static String formatDecimalWithZero(float f, int newScale) {
BigDecimal b = new BigDecimal(Float.toString(f));
double d = b.doubleValue();
String pattern = "0.";
for (int i = 0; i < newScale; i++) {
pattern += "0";
}
DecimalFormat df = new DecimalFormat(pattern);
return df.format(d);
}
/**
* 按四舍五入保留指定小数位数,位数不够用0补充
* @param d 格式化前的小数 String形式
* @param newScale 保留小数位数
* @return 格式化后的小数
*/
public static String formatDecimalWithZero(String d, int newScale) {
String pattern = "0.";
for (int i = 0; i < newScale; i++) {
pattern += "0";
}
DecimalFormat df = new DecimalFormat(pattern);
return df.format(Double.valueOf(d));
}
/**
* 按四舍五入保留指定小数位数,小数点后仅保留有效位数
* @param d 格式化前的小数
* @param newScale 保留小数位数
* @return 格式化后的小数
*/
public static String formatDecimal(double d, int newScale) {
String pattern = "#.";
for (int i = 0; i < newScale; i++) {
pattern += "#";
}
DecimalFormat df = new DecimalFormat(pattern);
return df.format(d);
}
/**
* 按四舍五入保留指定小数位数,小数点后仅保留有效位数
* @param d 格式化前的小数
* @param newScale 保留小数位数
* @return 格式化后的小数
*/
public static String formatDecimal(String d, int newScale) {
String pattern = "#.";
for (int i = 0; i < newScale; i++) {
pattern += "#";
}
DecimalFormat df = new DecimalFormat(pattern);
return df.format(Double.valueOf(d));
}
/**
* 按指定舍入模式保留指定小数位数
* @param d 格式化前的小数
* @param newScale 保留小数位数
* @param roundingMode 舍入模式
* (RoundingMode.UP始终进一/DOWN直接舍弃/
* CEILING正进负舍/FLOOR正舍负进/
* HALF_UP四舍五入/HALF_DOWN五舍六进/
* HALF_EVEN银行家舍入法/UNNECESSARY抛出异常)
* @return 格式化后的小数
*/
public static double formatDecimal(double d, int newScale, RoundingMode roundingMode) {
BigDecimal bd = new BigDecimal(d).setScale(newScale, roundingMode);
return bd.doubleValue();
}
/**
* 按指定舍入模式保留指定小数位数
* @param d 格式化前的小数
* @param newScale 保留小数位数
* @param roundingMode 舍入模式
* (RoundingMode.UP始终进一/DOWN直接舍弃/
* CEILING正进负舍/FLOOR正舍负进/
* HALF_UP四舍五入/HALF_DOWN五舍六进/
* HALF_EVEN银行家舍入法/UNNECESSARY抛出异常)
* @return 格式化后的小数
*/
public static double formatDecimal(String d, int newScale, RoundingMode roundingMode) {
BigDecimal bd = new BigDecimal(Double.valueOf(d)).setScale(newScale, roundingMode);
return bd.doubleValue();
}
}
我的博客:博客传送门
我的简书:简书传送门
我的CSDN:CSDN传送门
我的GitHub:GitHub传送门