1.EdiTtext輸入框控制不能輸入空格,給EditText添加一個addTextChangedListener監(jiān)聽,如果有空格split截取截取再for循環(huán)將截取后不包含空格的字符串數(shù)組重新排列這樣這個字符串就不包含空格了,最后將這個字符串重新寫入EditText,這時會出現(xiàn)一個問題就是光標會自動跳轉(zhuǎn)到第一個位置,在onTextChanged中會有一個叫做start的變量他會傳入在這個空格輸入之前的光標位置,EditText.setSelection(int)來改變光標的位置具體位置。
具體代碼:
private EditText edittextcll;// 輸入框 edittextcll = (EditText) findViewById(R.id.edittextcll); //監(jiān)聽輸入框禁止輸入空格 edittextcll.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (s.toString().contains(" ")) { String[] str = s.toString().split(" "); String str1 = ""; for (int i = 0; i < str.length; i++) { str1 += str[i]; } edittextcll.setText(str1); edittextcll.setSelection(start); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } });