发布于 2017-09-25 22:47:42 | 201 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


下面小编就为大家带来一篇Android 限制edittext 整数和小数位数 过滤器(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

写了一个过滤器,根据需要限制edittext输入的整数和小数位,如下代码:


package allone.verbank.apad.client.component;

import android.text.InputFilter;
import android.text.Spanned;

/**
 * 
 * @Title: ComponentDigitCtrlFilter.java 
 * @Package allone.verbank.apad.client.component 
 * @Description: 为了限制edit根据商品输入指定的位数和小数位
 * @author qiulinhe qiu.linhe@allone.cn 
 */
public class ComponentDigitCtrlFilter implements InputFilter {

 private boolean isJPY;
 private int digit;

 public ComponentDigitCtrlFilter(boolean isJPY, int digit) {
 this.isJPY = isJPY;
 this.digit = digit;
 }

 @Override
 public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
 // 删除等特殊字符,直接返回
 if ("".equals(source.toString())) {
 return null;
 }
 String oriValue = dest.toString();
 StringBuffer sb = new StringBuffer(oriValue);
 sb.append(source);
 String newValue = sb.toString();
 String[] newValueVec = newValue.split("\\.");
 if (newValueVec.length == 2) {
 double number = Double.parseDouble(newValueVec[0]);
 boolean numberflag = true;
 if (isJPY) {
 numberflag = ((number - 999 > 0.000001) ? false : true);
 } else {
 numberflag = ((number - 99 > 0.000001) ? false : true);
 }

 boolean digitflag = true;
 try {
 String digitNumber = newValueVec[1];
 digitflag = digitNumber.toCharArray().length > digit ? false : true;
 } catch (Exception ex) {
 digitflag = false;
 }
 if (numberflag && digitflag) {
 return source;
 } else {
 return "";
 }
 } else {
 double value = Double.parseDouble(newValue);
 if (isJPY) {
 return value > 999 ? "" : source;
 } else {
 return value > 99 ? "" : source;
 }
 }
 // dest.subSequence(dstart, dend)
 }
}

逻辑是判断传入的isJPY是否是要整数两位小数三位数的,然后对输入的数据进行限制,只需要将过滤器添加到对应的edittext控件即可,如下:stopEditText.setFilters(new InputFilter[] { new ComponentDigitCtrlFilter(digit == 2, digit) });

以上这篇Android 限制edittext 整数和小数位数 过滤器(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持PHPERZ。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务