Android之常用功能代码
透明导航栏
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);//透明导航栏
}
Toast的用法
Toast.makeText(MainActivityRegister.this, "这是一个Toast", Toast.LENGTH_SHORT).show();
检测邮箱格式类
public class CheckEmailFormat {
public boolean isValidEmail(String mail) {
Pattern pattern = Pattern.compile("^[A-Za-z0-9][\\w\\._]*[a-zA-Z0-9]+@[A-Za-z0-9-_]+\\.([A-Za-z]{2,4})");
Matcher mc = pattern.matcher(mail);
return mc.matches();
}
}
获取时间类(若要获取十二小时制的时间,只要把HH改成hh即可)
public class GetTime {
public String time(){
SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sDateFormat.format(new java.util.Date());
}
}
检测网络是否连通类
public class InternetStatus {
//用于判断网络连接是否可用
public boolean isNetworkEnable(Context temp_context) {
ConnectivityManager cm = (ConnectivityManager) temp_context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info=cm.getActiveNetworkInfo();
if(info!=null&&info.isAvailable()) {
return true;//网络连接可用
}
else {
return false;//网络连接不可用
}
}
}
文本框点击后获取焦点
public class EditTextFocus {
public void setFocus(EditText edit_text){
edit_text.setFocusable(true);
edit_text.setFocusableInTouchMode(true);
edit_text.requestFocus();
}
}
键盘相关
//强制显示键盘
public void showKeyboard(EditText temp_edit_text){
InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(temp_edit_text, InputMethodManager.SHOW_FORCED);
}
//强制隐藏键盘
public void hideKeyboard(Button temp_button){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(temp_button.getWindowToken(), 0);
}