為防止更新不及時,github地址如下:https://github.com/SolveBugs/NormalCode
1.給當前界面添加一個透明度
/**
* 給界面添加透明度
*
* @param activity
* @param bgAlpha
*/
public static void backgroundAlpha(Activity activity, float bgAlpha) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
activity.getWindow().setAttributes(lp);
}
2.一鍵添加qq群
http://qun.qq.com/join.html 選擇需要添加的群,然后選擇手機平臺挪拟,即可生成相應的代碼。
3.得到當前版本號
public static int getVersionCode(Context context) {
int code = 0;
if (context == null) {
return code;
} try {
PackageInfo info =context.getPackageManager().getPackageInfo(context.getPackageName(), 0); code = info.versionCode;
} catch (PackageManager.NameNotFoundException e) {
}
return code;
}
4.判斷當前網絡是否可用
public static boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager .getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable()
&& mNetworkInfo.isConnectedOrConnecting();
}
}
return false;
}
5.當前是否是移動網絡
public static boolean isMobileConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobileNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobileNetworkInfo != null) {
return mMobileNetworkInfo.isAvailable()
&& mMobileNetworkInfo.isConnectedOrConnecting();
}
}
return false;
}
6.當前是否是wifi
public static boolean isWifi(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager .getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI;
}
}
return false;
}
7.ListView根據item計算出實際的高度
public static void setListViewHeightBasedOnChildren(ListView listView) {
// 獲取ListView對應的Adapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0); // 計算子項View 的寬高
totalHeight += listItem.getMeasuredHeight(); // 統(tǒng)計所有子項的總高度
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
8.撥打電話(記得在清單文件添加權限)
/**
* 給界面添加透明度
* @param phoneNum
*/
public void call(String phoneNum){
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ phoneNum));
startActivity(intent);
}
- 字符串是否包含漢字
public static boolean checkChinese(String sequence) {
final String format = "[\\u4E00-\\u9FA5\\uF900-\\uFA2D]";
boolean result = false;
Pattern pattern = Pattern.compile(format);
Matcher matcher = pattern.matcher(sequence);
result = matcher.find();
return result;
}
10.從assets 文件夾中讀取圖片
public static Drawable loadImageFromAsserts(final Context ctx, String fileName) { try {
InputStream is = ctx.getResources().getAssets().open(fileName);
return Drawable.createFromStream(is, null);
} catch (IOException e) {
if (e != null) {
e.printStackTrace();
}
} catch (OutOfMemoryError e) { i
if (e != null) {
e.printStackTrace();
}
} catch (Exception e) {
if (e != null) {
e.printStackTrace();
}
}
return null;
}
11.判斷字符串是否為空
public static boolean isNull(String string) {
if (string != null) {
string = string.trim();
if (string.length() != 0) {
return false;
}
}
return true;
}