欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android的EditText字數(shù)檢測和限制解決辦法

 更新時間:2017年03月07日 15:40:27   投稿:lqh  
這篇文章主要介紹了Android的EditText字數(shù)檢測和限制解決辦法的相關(guān)資料,需要的朋友可以參考下

Android的EditText字數(shù)檢測和限制解決辦法

控件EditText在Android布局中經(jīng)常用到,對EditText中輸入的內(nèi)容也經(jīng)常需要進行限制,我們可以通過TextWatcher去觀察輸入框中輸入的內(nèi)容。

public class TextWatcherDemo extends Activity {
  private TextView mTextView;
  private EditText mEditText;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mTextView = (TextView)findViewById(R.id.tv);
    mEditText = (EditText)findViewById(R.id.ET);
    mEditText.addTextChangedListener(mTextWatcher);
  }
  TextWatcher mTextWatcher = new TextWatcher() {
    private CharSequence temp;
    private int editStart ;
    private int editEnd ;
    @Override
    public void beforeTextChanged(CharSequence s, int arg1, int arg2,
        int arg3) {
      temp = s;
    }
    
    @Override
    public void onTextChanged(CharSequence s, int arg1, int arg2,
        int arg3) {
      mTextView.setText(s);
    }
    
    @Override
    public void afterTextChanged(Editable s) {
      editStart = mEditText.getSelectionStart();
      editEnd = mEditText.getSelectionEnd();
      if (temp.length() > 10) {
        Toast.makeText(TextWatcherDemo.this,
            "你輸入的字數(shù)已經(jīng)超過了限制!", Toast.LENGTH_SHORT)
            .show();
        s.delete(editStart-1, editEnd);
        int tempSelection = editStart;
        mEditText.setText(s);
        mEditText.setSelection(tempSelection);
      }
    }
  };
}

關(guān)于android中的編碼

result.getBytes() 是 new String(byte[]) 的逆過程。

前面那個是 String->byte[] ,后面那個是 byte[] -> String.

在Java運行時的時候,String與String是沒有區(qū)別的都是以2字節(jié)的unicode的形式存在內(nèi)存中,所謂編碼,是針對把String轉(zhuǎn)換成 byte[]而言的。比如我可以把 "abc" 通過 utf-8轉(zhuǎn)換成了一串數(shù)據(jù) A ,也可以通過gb2312轉(zhuǎn)換成另一串數(shù)據(jù) B,這個過程就是 String.getBytes(),比如 "abc".getBytes("utf-8")得到A , "abc".getBytes("gb2312")得到B。如果是"abc".getBytes(),就不知道用的什么編碼了,這和平臺相關(guān)。

那如何從A串或者 B串重新得到String呢,那就是 new String(A,"utf-8") 或者 new String(B,"gb2312")。因為A是從utf-8轉(zhuǎn)換得到的,所以用utf-8轉(zhuǎn)回String ,如果new String(A,"gb2312"), 那么其中的中文就是亂碼。

下面列出各編碼格式下字符的字節(jié)數(shù):

英文字母:A

字節(jié)數(shù):1;編碼:GB2312 
字節(jié)數(shù):1;編碼:GBK 
字節(jié)數(shù):1;編碼:GB18030 
字節(jié)數(shù):1;編碼:ISO-8859-1 
字節(jié)數(shù):1;編碼:UTF-8 
字節(jié)數(shù):4;編碼:UTF-16 
字節(jié)數(shù):2;編碼:UTF-16BE 
字節(jié)數(shù):2;編碼:UTF-16LE 

中文漢字:人

字節(jié)數(shù):2;編碼:GB2312 
字節(jié)數(shù):2;編碼:GBK 
字節(jié)數(shù):2;編碼:GB18030 
字節(jié)數(shù):1;編碼:ISO-8859-1 
字節(jié)數(shù):3;編碼:UTF-8 
字節(jié)數(shù):4;編碼:UTF-16 
字節(jié)數(shù):2;編碼:UTF-16BE 
字節(jié)數(shù):2;編碼:UTF-16LE 

根據(jù)上面的結(jié)果,我們可以通過每個字符的UTF-8字節(jié)數(shù)來判斷是中文還是英文。

工作中遇到一個需求,是要限制EditText中輸入的字符數(shù)的個數(shù),中文15個,英文30個,中英文會交叉輸入,就可以用上面的條件來判斷。

具體的實現(xiàn)如下:

 private TextWatcher mInputTextWatcher = new TextWatcher() {
    private String temp;
    private int editStart;
    private int editEnd;

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
      temp = s.toString();
    }

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

    @Override
    public void afterTextChanged(Editable s) {
      mMainHandler.removeMessages(HD_MSG_UPDATE_HINT);
      mCurrentHint = s.toString().trim();

      if (!TextUtils.isEmpty(temp)) {
        String limitSubstring = getLimitSubstring(temp);
        if (!TextUtils.isEmpty(limitSubstring)) {
          if (!limitSubstring.equals(temp)) {
            // Toast.makeText(activity, "字數(shù)已超過限制",
            // Toast.LENGTH_SHORT).show();
            mEdtInput.setText(limitSubstring);
            mEdtInput.setSelection(limitSubstring.length());
          }
        }
      }
      mMainHandler.sendEmptyMessageDelayed(HD_MSG_UPDATE_HINT, HINT_UPDATE_DALEY_TIME);
    }
  };

  

private String getLimitSubstring(String inputStr) {
    int orignLen = inputStr.length();
    int resultLen = 0;
    String temp = null;
    for (int i = 0; i < orignLen; i++) {
      temp = inputStr.substring(i, i + 1);
      try {// 3 bytes to indicate chinese word,1 byte to indicate english
         // word ,in utf-8 encode
        if (temp.getBytes("utf-8").length == 3) {
          resultLen += 2;
        } else {
          resultLen++;
        }
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }
      if (resultLen > 30) {
        return inputStr.substring(0, i);
      }
    }
    return inputStr;
  }

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論