Android Tips(二)

判断应用是否联网

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 判断是否联网
*
* @param context
* @return boolean true,已联网;false,断网状态
*/

public static boolean isNetwork(Context context) {
try {
ConnectivityManager manger = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manger.getActiveNetworkInfo();
return (info != null && info.isConnected());
} catch (Exception e) {
Log.e(TAG, "判断是否有网络出错");
return false;
}
}

判断字符是否中文

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* @Description: 判断字符是否中文
* @param c
* 字符
* @return boolean
* @author
* @data
*/
public boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}

显示/隐藏软键盘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* 显示软键盘 */
public void ShowSoftKeyborad(Context context) {
InputMethodManager inputMethodManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(this,
InputMethodManager.RESULT_UNCHANGED_SHOWN);
}

/* 隐藏软键盘 */
public void hideSoftKeyborad(Context context) {
InputMethodManager inputMethodManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getWindowToken(),
InputMethodManager.RESULT_UNCHANGED_HIDDEN);
}

热评文章