android常用工具類集合

2021-07-22 06:53:56 字數 3619 閱讀 9416

本文集合本人在開發中經常用到的工具類(待續):

1、獲取系統日期時間

更詳細的使用詳見:[日期時間操作類工具]

2、日期格式字串轉換成時

public

static string date2timestamp(string date_str) catch (exception e)

return

""; }

3、檢查網路是否可用

public

static

boolean

isnetworkconnected(context context) }}

}return

false;

}

4、將string型別的陣列轉化為逗號間隔字串形式

public

static string stringarraytostring(string stringarray)

return arrays.tostring(stringarray).replaceall("^\\[| |\\]$", "");

}

5、最省記憶體方式讀取資源

public

static bitmap readbitmap(context context, int resid)

6、判斷字串是否為空

public

public

static boolean stringisempty(string string)

if (string.trim().equals("") || string.trim().equals("null"))

return

false;

}

7、判斷字串是否為數字

public

static

boolean

isnumeric1(string str)

8、判斷是否是正確的郵箱格式

public static boolean matchemail(string email)
9、判斷是否是手機格式

public static boolean matchphone(string phone) 

$)|(^0[1,2]

\\d-?\\d$)|(^0[3-9]

\\d-?\\d

$)|(^0[1,2]

\\d-?\\d-(\\d)$)|(^0[3-9]

\\d-? \\d-(\\d)$))";

string expression = "((^(10|11|12|13|14|15|16|17|18|19)[0-9]

$)|(^0[1,2]

\\d-?\\d

$)|(^0[3-9]

\\d-?\\d$)|(^0[1,2]

\\d-?\\d-(\\d)$)|(^0[3-9]

\\d-? \\d-(\\d)$))";

charsequence inputstr = phone;

pattern pattern = pattern.compile(expression);

matcher matcher = pattern.matcher(inputstr);

if (matcher.matches())

return isvalid;

}

10、隱藏軟鍵盤

public

static

void

hidekeyboard(context context, ibinder binder)

11、android中px、dp、sp等轉換

public

static

intpx2dip(context context, float pxvalue)

public

static

intdip2px(context context, float dipvalue)

public

static

intpx2sp(context context, float pxvalue)

public

static

intsp2px(context context, float spvalue)

public

static

float

value)

return

0; }

12、全域性toast

private

static toast toast;

@suppresslint("showtoast")

private

static

void

gettoast(context context)

if (view == null)

toast.setview(view);

}//全域性toast

public

static

void

showtoast(context context, charsequence msg,

int duration) catch (exception e)

}

自定義toast

private

static toast toast;

public

static

void

showcustomertoast(final context c, string msg, int duration) else

}

13、獲取android系統版本號

public

static

intgetsdkversionnumber() catch (numberformatexception e)

return sdkversion;

}

14、獲取所在資料夾

public

static string getdir(string path)

15、判斷gps是否可用

public

static

boolean

isuseablegps(context mcon) else

}

16、獲取程式版本號

public

static string getversionname(context mcon) catch (namenotfoundexception e)

}

17、獲取網速

private

static

boolean

isfastmobilenetwork(context context)

}

android常用工具類

import android.content.context import android.net.connectivitymanager import android.net.networkinfo import android.widget.toast 常用的工具類集合!public class...

Android開發常用工具類

該類包含bitmap常用的工具類,涉及到bitmap的獲取 bitmap與drawable的相互轉換。該類包含日期的常用處理,涉及到日期字串與date物件的各種格式相互轉換 獲取星期 年月份 天數等功能。補充內部類datepattern 為我所定義的日期格式的列舉類。常用的json工具類,包含jso...

常用工具類

本講內容 常用工具類 陸續更新 author administrator public static context context public void oncreate public static context getcontext 2 提供所有activity呼叫 基類 author ad...