Android开发中一些常用方法(持续更新)

mac2026-03-11  5

       在项目中我们会经常需要一些工具类或者工具方法,把自己在学习或者工作会遇到的一些方法记录下来,方便自己去查找。

输入框不可编辑:

/** * 设置EditText是否可编辑 * @param editText EditText对象 * @param mode true:可编辑 false:不可编辑 */ private void setEditTextEnable(EditText editText,boolean mode){ editText.setFocusable(mode); editText.setFocusableInTouchMode(mode); editText.setLongClickable(mode); editText.setInputType(mode ? InputType.TYPE_CLASS_TEXT:InputType.TYPE_NULL); }

手机号用****号隐藏中间数字:

/** * 手机号用****号隐藏中间数字 * @param phone * @return */ public static String settingphone(String phone) { String phone_s = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"); return phone_s; }

获取Mac地址:

private static String getMacFromHardware() { try { List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface nif : all) { if (!nif.getName().equalsIgnoreCase("wlan0")) continue; byte[] macBytes = nif.getHardwareAddress(); if (macBytes == null) { return ""; } StringBuilder res1 = new StringBuilder(); for (byte b : macBytes) { res1.append(String.format("%02X:", b)); } if (res1.length() > 0) { res1.deleteCharAt(res1.length() - 1); } return res1.toString(); } } catch (Exception e) { e.printStackTrace(); } return "02:00:00:00:00:00"; }

时间格式:

获取当前时间yyMMdd格式 /** * 获取当前时间yyMMdd格式 */ public String getTimeData() { long currentTime = System.currentTimeMillis(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMdd"); String startDate = simpleDateFormat.format(currentTime); return startDate; } 获取当前时间yyMMdd  hh:mm:ss格式  new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new java.util.Date())

判断手机号码:

/** * 判断手机号码 * @param mobile */ public static boolean isMobile(String mobile) { String regex = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$"; Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(mobile); return m.matches(); }

 

最新回复(0)