android代码集EditText只要输入号码、信
如何設置EditText,因此,只有某些數字或字母可以進入它?
一、建立EditText,只要輸入號碼:
辦法1:直接生成DigitsKeyListener了。
et_1.setKeyListener(new
DigitsKeyListener(false,true));
方法2:在EditText中設置屬性。android:numeric="integer"即僅僅能輸入整數。例如以下
android:singleLine="true"
android:numeric="integer"
/>
方法3:新建一個char[]。在里面加入同意輸入的字符。
例如以下
editText.setKeyListener(new
NumberKeyListener(){
protected char[] getAcceptedChars()
{
char[]
numberChars[]={'1','2','3','4','5','6','7','8','9','0',};
return numberChars;
}
});
二、設置EditText僅僅能輸入某些字母,如以下設置edtitext僅僅能輸入A—N,a—n這些字母。
方法例如以下:
editText.setKeyListener(new
NumberKeyListener(){
protected char[] getAcceptedChars()
{
char[]
numberChars[]={'a,'b','c','d','e','f','A','B','C','D'};
return numberChars;
}
});
EditText et;et = (EditText) findViewById(R.id.et);// 方法1:建立一個DigitsKeyListener,然后把它設為你的EditText的KeyListenerDigitsKeyListener numericOnlyListener = new
DigitsKeyListener(false,true);et.setKeyListener(numericOnlyListener);//
方法2:為EditText設置一個NumberKeyListener,然后重寫getAcceptedChars()方法和getInputType()方法et.setKeyListener(new NumberKeyListener() {@Overrideprotected char[] getAcceptedChars() {return new char[] { '1', '2', '3', '4', '5', '6', '7', '8','9', '0'
};}@Overridepublic int getInputType() {// TODO Auto-generated method stubreturn android.text.InputType.TYPE_CLASS_PHONE;}});
--------------------------------------------------------------------------------------------
01.EditText et;02.et = (EditText) findViewById(R.id.et);03.// 方法1:建立一個DigitsKeyListener,然后把它設為你的EditText的KeyListener04.DigitsKeyListener numericOnlyListener = new
DigitsKeyListener(false,true);05.et.setKeyListener(numericOnlyListener);06.//
方法2:為EditText設置一個NumberKeyListener,然后重寫getAcceptedChars()方法和getInputType()方法07.et.setKeyListener(new NumberKeyListener() {08. @Override09. protected char[] getAcceptedChars() {10. return new char[] { '1', '2', '3', '4', '5', '6', '7', '8','9',
'0' };11. }12. @Override13. public int getInputType() {14. // TODO Auto-generated method stub15. return android.text.InputType.TYPE_CLASS_PHONE;16. }17.});
小結:
第一種能夠輸入小數。
另外一種因為設置了TYPE_CLASS_PHONE所以僅僅能輸入整數。
且比較靈活。
============================================
非常多網友可能在開發Android時發現EditText有時候須要限制用戶輸入的內容,通常我們能夠使用正則表達式直接限制,可是Android
已經為我們準備好了EditText的輸入類型,這種比正則要有下面幾點優勢:
1. 開發更簡單,運行速度高效。 2.
輸入法默認會依據情況變動。比方說設置為numeric后輸入法會自己主動僅顯示數字,不會出現Qwerty中的字母。
以下我們通過EditText的layout
xml文件里的相關屬性來實現:
1. 密碼框屬性 android:password="true"
這條能夠讓EditText顯示的內容自己主動為 星號,輸入時內容會在1秒內變成*字樣。
2. 純數字 android:numeric="true"
這條能夠讓輸入法自己主動變為數字輸入鍵盤,同一時候僅同意0-9的數字輸入
3. 僅同意 android:capitalize="cwj1987"
這樣僅同意接受輸入cwj1987。一般用于password驗證
以下是一些擴展的風格屬性
android:editable="false"
設置EditText不可編輯
android:singleLine="true"
強制輸入的內容在單行
android:ellipsize="end"
自己主動隱藏尾部溢出數據,它通常用于當文本太長,一行不能全部展示。
版權聲明:本文博主原創文章,博客,未經同意不得轉載。
轉載于:https://www.cnblogs.com/zfyouxi/p/4844970.html
總結
以上是生活随笔為你收集整理的android代码集EditText只要输入号码、信的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android xml组建圆角背景设置
- 下一篇: 由于问题引起信号ORA-27154无法启