版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_30299243/article/details/83714411
对于一个多人合作的项目来讲,每个人都有自己的代码风格,为了增加代码的可读性和后期的升级。我的建议是把一些重复的代码封装一下。写上注释,这样,其他人也可以调用。废话上说。直接代码。
/** * 沉浸式 * * @param activity */ public static void Immersive(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } }
调用:
@Override protected void onCreate(Bundle savedInstanceState) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } super.onCreate(savedInstanceState); }
/** * 判断手机号格式 * * @param telephone * @return */ public static boolean isTrueTelePhone(String telephone) { Pattern p = Pattern.compile("^((13[0-9])|(15[0-9])|(18[0-9])|(17[0-9])|(16[0-9])|(14[0-9]))\\d{8}$"); Matcher m = p.matcher(telephone); if (m.matches()) { return true; } return false; }
/** * 座机判断 * * @param str 传入的字符串 * @return 真假 */ public static boolean IsTelephone(String str) { String regex = "^0(10|2[0-5789]-|\\d{3})-?\\d{7,8}$"; return matches(regex, str); }
/** * @param context * @return 获取App版本号 */ public static String getVersionName(Context context) { try { PackageManager packageManager = context.getPackageManager(); PackageInfo packageInfo = packageManager.getPackageInfo( context.getPackageName(), 0); String a = packageInfo.versionName; return a; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return "0.0"; } /** * 拨打电话 * * @param phonenumber * @param context */ public static void call(String phonenumber, Context context) { if (!phonenumber.equals("")) { Intent callPhone = new Intent(); callPhone.setAction(Intent.ACTION_DIAL); callPhone.setData(Uri.parse("tel:" + phonenumber));//电话号码 context.startActivity(callPhone); } else { //UiUtils.showToast(context, "无法获取电话号码"); } } /** * 获取字符长度 * * @param s * @return */ public static int getWordCount(String s) { int length = 0; for (int i = 0; i < s.length(); i++) { int ascii = Character.codePointAt(s, i); if (ascii >= 0 && ascii <= 255) length++; else length += 2; } return length; } /** * 打开软键盘 * * @param mEditText * @param mContext */ public static void openKeybord(final EditText mEditText, Context mContext) { mEditText.requestFocus(); InputMethodManager imm = (InputMethodManager) mContext.getSystemService(mContext.INPUT_METHOD_SERVICE); imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); } /** * 关闭软键盘 * * @param mEditText * @param mContext */ public static void closeKeybord(EditText mEditText, Context mContext) { InputMethodManager imm = (InputMethodManager) mContext .getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0); } public static boolean isEmpty(String value) { if (value != null && value.trim().length() != 0) { return true; } return false; }
/** * 时间戳转化为日期字符串 * * @param seconds * @return */ public static String timeStamp2Date(String seconds) { if (seconds == null || seconds.isEmpty() || seconds.equals("null")) { return ""; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(new Date(Long.valueOf(seconds + "000"))); } /** * 日期格式转化成时间戳 * * @param date_str * @return */ public static String date2TimeStamp(String date_str) { try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return String.valueOf(sdf.parse(date_str).getTime() / 1000); } catch (Exception e) { e.printStackTrace(); } return ""; } /** * 取得当前时间戳(精确到秒) * * @return */ public static String timeStamp() { long time = System.currentTimeMillis(); String t = String.valueOf(time / 1000); return t; }
/** * 设置textview部分文字的颜色 * * @param textView * @param start * @param end */ public static void setColorText(TextView textView, int start, int end, int colorid, Context context) throws RuntimeException { SpannableStringBuilder builder = new SpannableStringBuilder(textView.getText().toString()); ForegroundColorSpan color = new ForegroundColorSpan(context.getResources().getColor(colorid)); builder.setSpan(color, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(builder); } /** * 更改textview字体颜色 * * @param textView 需要更改的控件 * @param s 颜色的string值 */ public static void changecolor(TextView textView, String s) { textView.setBackgroundColor(Color.parseColor(s)); }
/** * 时分选择 可以根据需要传递已经有的时间 * * @param newhour 传递的时间小时 * @param newmin 传递的时间分钟 * @param context 上下文 * @param textView 赋值的控件 */ public static void choosetime(int newhour, int newmin, Context context, final TextView textView) { final String[] hour = new String[1]; final String[] min = new String[1]; new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { if (hourOfDay < 10) { hour[0] = "0" + hourOfDay; } else { hour[0] = hourOfDay + ""; } if (minute < 10) { min[0] = "0" + minute; } else { min[0] = "" + minute; } // textView.setText(String.format("%d:%d", hourOfDay, minute) + ""); textView.setText(hour[0] + ":" + min[0]); } //0,0指的是时间,true表示是否为24小时,true为24小时制 }, newhour, newmin, true).show(); } /** * 回调返回 * * @param context 上下文 * @param name 传递的参数 * @param status 状态(自定义) */ public static void getback(Activity context, String name, String status) { Intent intent = new Intent(); intent.putExtra("name", name); intent.putExtra("status", status); //把两个数据传递过去 context.setResult(RESULT_OK, intent); //把结果返回去 context.finish(); } /** * 截取字符串 例如:123 * * @param cstring 需要截取的字符串 123 * @param start 开始位置 1 * @param end 结束位置 3 * @return 返回新的字符串 2 */ public static String InterceptString(String cstring, int start, int end) { String newstring = ""; newstring = cstring.substring(start, end); return newstring; } /** * 字符串截取 * String s = "3#13666666666/010-69854412*400-98654565"; * String s1 = s.substring(s.indexOf("/")+1,s.indexOf("*")); * 输出 010-69854412 * * @param s 传入的字符串 * @param start 开始位置 * @param end 结束位置 * @return 把新的字符串返回去 */ public static String subStrings(String s, int start, int end) { String newstring = ""; newstring = s.substring(start, end); return newstring; }