Android之EditText控制禁止輸入空格和回車
1.EdiTtext輸入框控制不能輸入空格,給EditText添加一個addTextChangedListener監(jiān)聽,如果有空格split截取截取再for循環(huán)將截取后不包含空格的字符串?dāng)?shù)組重新排列這樣這個字符串就不包含空格了,最后將這個字符串重新寫入EditText,這時會出現(xiàn)一個問題就是光標(biāo)會自動跳轉(zhuǎn)到第一個位置,在onTextChanged中會有一個叫做start的變量他會傳入在這個空格輸入之前的光標(biāo)位置,EditText.setSelection(int)來改變光標(biāo)的位置具體位置。
具體代碼:
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) {
}
});
2.EditText控制不能輸入回車,在xml中添加android:singleLine="true"
<EditText android:id="@+id/edittextcll" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="@null" android:gravity="top" android:hint="輸入更多補充意見" android:singleLine="true" android:textColor="#262626" android:textSize="16dp" />
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
Android實現(xiàn)動態(tài)自動匹配輸入內(nèi)容功能
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)動態(tài)自動匹配輸入內(nèi)容功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
Android模擬實現(xiàn)華為系統(tǒng)升級進(jìn)度條
這篇文章主要介紹了如何通過Android模擬實現(xiàn)華為在系統(tǒng)升級時顯示的進(jìn)度條。文中的實現(xiàn)過程講解詳細(xì),感興趣的小伙伴可以動手試一試2022-01-01
Android 自定義布局豎向的ViewPager的實現(xiàn)
這篇文章主要介紹了Android 自定義布局豎向的ViewPager的實現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android中父View和子view的點擊事件處理問題探討
當(dāng)屏幕中包含一個ViewGroup,而這個ViewGroup又包含一個子view,這個時候android系統(tǒng)如何處理Touch事件呢,接下來將對此問題進(jìn)行深入了解,感興趣的朋友可以了解參考下,或許對你有所幫助2013-01-01

