edit输入框内容格式判断等各种判断工具

public class Tools {

private static Context context;

/** 
 * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 
 */  
public static int dip2px(Context context, float dpValue) {  
    final float scale = context.getResources().getDisplayMetrics().density;  
    return (int) (dpValue * scale + 0.5f);  
}  

/** 
 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp 
 */  
public static int px2dip(Context context, float pxValue) {  
    final float scale = context.getResources().getDisplayMetrics().density;  
    return (int) (pxValue / scale + 0.5f);  
}   

/**
 * 保留小数点后两位
 * @param days 租赁的天数
 * @param money 价格
 * @return
 */
public static String getTwoDecimal(int days ,double money) {
    DecimalFormat df = new DecimalFormat("#####0.00");
    return df.format(days*money);
}

/**
 * 获取运行在前台的activity
 * @param context
 * @return
 */
public static Activity getFrontActivity(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE); 
    List<RunningTaskInfo> cn = am.getRunningTasks(1); 
    RunningTaskInfo taskInfo = cn.get(0); 
    ComponentName name = taskInfo.topActivity; 
    Activity foregroundActivity;
    try {
        foregroundActivity = (Activity) (Class.forName(name.getClassName()).newInstance());
        return foregroundActivity; 
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null; 
}

/**
 * 判断某个activity是否运行在前台 true为运行在前台
 * @param context
 * @param className
 * @return
 */
public static boolean activityManager(Context context, String className) {
     ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);  
     List<RunningTaskInfo> list = am.getRunningTasks(1);  
     if (list != null && list.size() > 0) {  
        ComponentName cpn = list.get(0).topActivity;  
        if (className.equals(cpn.getClassName())) {  
           return true;  
        }  
     }
    return false;  
}

/**
 * 判断程序是否在后台运行
 * @param context
 * @return
 */
public static boolean isApplicationBroughtToBackground(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }
    return false;
}

/**
 * 输入框检测方法
 * 
 * @param s
 * @return
 */
public static boolean editTextChecker(String s) {
    Pattern p = Pattern.compile("\\w*");
    Matcher m = p.matcher(s);
    return m.matches();
}

/**
 * 实时监控EditText中的特殊字符
 * @param context
 * @param editText
 */
public static void verification_editText(Context context1 ,final EditText editText) {
    context = context1;
    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String editable = editText.getText().toString();

            //只能输入字母,数字和汉字
            String regEx = "[^a-zA-Z0-9\u4E00-\u9FA5_]"; 
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(editable.toString());
            String str = m.replaceAll("").trim();

            if(!editable.equals(str)){
                editText.setText(str);
                editText.setSelection(str.length()); //光标置后
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

/**
 * 判断是不是纯数字
 * @param pwd
 * @return
 */
public static boolean numType(String pwd) {
    boolean s = true;

    for(int i = 0;i<pwd.length();i++){
        if (!Character.isDigit(pwd.charAt(i))) {
            s = false;
        }
    }
    return s;
}

/**
 * 判断密码是否为数字和字母
 * @param pwd
 * @return
 */
public static boolean pwdType(String pwd) {
    boolean digit = false;
    boolean letter = false;
    int p = pwd.length();
    for(int i=0;i<p;i++){
        if (Character.isDigit(pwd.charAt(i))) {
            digit = true;
        } else if (Character.isLetter(pwd.charAt(i))) {
            letter = true;
        }
    }
    if (digit && letter) {
        return true;
    } else {
        return false;
    }
}

/**
 * 关闭软键盘
 * @param activity
 */
public static void hideSoftInput(Activity activity) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    if(imm.isActive() && activity.getCurrentFocus() != null){
        imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

/**
 * 切换后将EditText光标置于末尾
 * @param et
 */
public static void tailCursor(EditText et) {
    CharSequence charSequence = et.getText();
    if (charSequence instanceof Spannable) {
        Spannable spanText = (Spannable) charSequence;
        Selection.setSelection(spanText, charSequence.length());
    }
}

/**
 * 正则表达式 判断邮箱格式是否正确
 * @param email
 * @return
 */
public static boolean isEmail(String email) {
    String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    Pattern p = Pattern.compile(str);
    Matcher m = p.matcher(email);
    return m.matches();
}

/**
 * 判断手机格式是否正确
 * @param mobiles
 * @return
 */
public static boolean isphone(String mobiles) {
    Pattern p = Pattern.compile("^((13[0-9])|(14[5])|(15[^4,\\D])|(17[0-9])|(18[0-9]))\\d{8}$");
    Matcher m = p.matcher(mobiles);
    return m.matches();
}

/**
 * //只能输入字母,数字和汉字
 * @param str
 * @return
 */
public static String stringFilter(String str) {
    String regEx = "[^a-zA-Z0-9\u4E00-\u9FA5_]"; 
    Pattern p = Pattern.compile(regEx);
    Matcher m = p.matcher(str);
    return m.replaceAll("").trim();
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值