美文网首页
MaxLengthWatcher 设置50个字节(一个汉字2个字

MaxLengthWatcher 设置50个字节(一个汉字2个字

作者: 勤劳的蚂蚁 | 来源:发表于2020-03-19 11:48 被阅读0次

import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.widget.EditText;

import java.io.UnsupportedEncodingException;

/*
 * 监听输入内容是否超出最大长度,并设置光标位置
 * */
public class MaxLengthWatcher implements TextWatcher {

    private int maxLen = 0;
    private EditText editText = null;


    public MaxLengthWatcher(int maxLen, EditText editText) {
        this.maxLen = maxLen;
        this.editText = editText;
    }

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                  int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        if(maxLen<=0){
           return;
        }

    //字符长度 一个中文两个字符
        Editable editable = editText.getText();
        int len = editable.length();
        if(len > maxLen>>2){//大于一半的时候判断
            try {
                String str = editable.toString();
                byte[] bytes = str.getBytes("GBK");
                if (bytes.length >= maxLen) {
                    int selEndIndex = Selection.getSelectionEnd(editable);
                    //创建新byte数组接受值
                    byte[] b1 = new byte[maxLen];
                    System.arraycopy(bytes, 0, b1, 0, maxLen);
                    //获取新字符串
                    String newStr = new String(b1,"GBK");
                    editText.setText(newStr);
                    editable = editText.getText();

                    //新字符串的长度
                    int newLen = editable.length();
                    //旧光标位置超过字符串长度
                    if(selEndIndex > newLen)
                    {
                        selEndIndex = editable.length();
                    }
                    //设置新光标所在的位置
                    Selection.setSelection(editable, selEndIndex);
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        //字符的个数
//        Editable editable = editText.getText();
//        int len = editable.length();
//        if(len > maxLen)
//        {
//            int selEndIndex = Selection.getSelectionEnd(editable);
//            String str = editable.toString();
//            //截取新字符串
//            String newStr = str.substring(0,maxLen);
//            editText.setText(newStr);
//            editable = editText.getText();
//
//            //新字符串的长度
//            int newLen = editable.length();
//            //旧光标位置超过字符串长度
//            if(selEndIndex > newLen)
//            {
//                selEndIndex = editable.length();
//            }
//            //设置新光标所在的位置
//            Selection.setSelection(editable, selEndIndex);
//
//        }
    }

}



相关文章

  • MaxLengthWatcher 设置50个字节(一个汉字2个字

  • 为什么需要UTF-8与GBK进行转化?

    UTF-8一个汉字是三个字节,英文是一个字节; GBK一个汉字是两个字节;英文是一个字节;

  • SAS Base

    变量名 名字的长度要小于等于 32 个字节。(一个字母 1 个字节, 一个汉字 2 个字节)以字母或下划线开头。可...

  • UTF8中中文占多少个字节

    占2个字节的:〇 占3个字节的:基本等同于GBK,含21000多个汉字 占4个字节的:中日韩超大字符集里面的汉字,...

  • String 类构造方法

    字节是负数=汉字 一个汉字连个字节 String(byte [] bytes,int offset, int l...

  • 一步一步学习 Web 安全 3.1 宽字节注入

    原理 宽窄字节字符大小为一个字节(英文默认一个字节)称为窄字节,为两个字节(汉字默认两个字节)的称为宽字节,比如:...

  • 计算机是怎样跑起来的

    存储汉字时,字符编码不同,汉字所占用的字节数也就不同。在GBK字符编码下,一个汉字占用2个字节。而在UTF-8字符...

  • iOS面试题及答案整理 — 持续更新

    一、基础篇 1 . 一个字节多少位?一个汉字多少位?一个字母多少位 一个汉字等于两个英文字母等于两个字节、一个字节...

  • 字节

    1字节=8位(bit) 一个汉字占2个字节,一个英文,阿拉伯数字占1个字节 二进制11111111 = 十进制的255,

  • 2020-ios-面试总结

    基础篇 1 . 一个字节多少位?一个汉字多少位?一个字母多少位一个汉字等于两个英文字母等于两个字节、一个字节是八位...

网友评论

      本文标题:MaxLengthWatcher 设置50个字节(一个汉字2个字

      本文链接:https://www.haomeiwen.com/subject/dxwkyhtx.html