当前位置: 代码迷 >> Android >> android下文本框输入限制最大字节数
  详细解决方案

android下文本框输入限制最大字节数

热度:26   发布时间:2016-05-01 17:26:24.0
android上文本框输入限制最大字节数
public class MaxByteLengthEditText extends EditText {		private int maxByteLength = 100;		private String encoding = "GBK";	public MaxByteLengthEditText(Context context) {		super(context);		init();	}		public MaxByteLengthEditText(Context context,AttributeSet attrs) {  		super(context,attrs);		init();	}		private void init() {		setFilters(new InputFilter[] {inputFilter});	}		public int getMaxByteLength() {		return maxByteLength;	}	public void setMaxByteLength(int maxByteLength) {		this.maxByteLength = maxByteLength;	}	public String getEncoding() {		return encoding;	}	public void setEncoding(String encoding) {		this.encoding = encoding;	}	/**	 * input输入过滤	 */	private InputFilter inputFilter = new InputFilter() {		@Override		public CharSequence filter(CharSequence source, int start, int end,				Spanned dest, int dstart, int dend) {            try {            	int len = 0;            	boolean more = false;            	do {            		SpannableStringBuilder builder =             			new SpannableStringBuilder(dest).replace(dstart, dend, source.subSequence(start, end));                    len = builder.toString().getBytes(encoding).length;                    more = len > maxByteLength;                    if (more) {                    	end--;                    	source = source.subSequence(start, end);                    }            	} while (more);                return source;            } catch (UnsupportedEncodingException e) {            	return "Exception";            }		}    };}
  相关解决方案