部分內容參考:http://www.cnblogs.com/hnrainll/archive/2012/11/24/2786159.html
控制EditText的輸入的時候:
1)常用設置
android:hint="請輸入數字!"//設置顯示默認的提示信息
android:focusable="false"// 無法輸入內容 android:singleLine="true"//設置單行輸入,一旦設置為true,則文字不會自動換行。 android:textColor = "#ff8c00"//字體顏色 android:textStyle="normal"//正常字體 ="bold" //粗體 ="italic"//斜體 ="bold|italic"//粗體+斜體 android:capitalize = "characters"//輸入內容為大寫字母(注意:可以用inputType的某個屬性代替) android:textColorHighlight="#cccccc"//被選中文字的底色,默認為藍色 android:textColorHint="#ffff00"//設置提示信息文字的顏色,默認為灰色 android:textScaleX="1.5"//控制字與字之間的間距 android:background="@null"//背景,這里沒有,指透明 android:layout_gravity="center_vertical"//設置控件顯示的位置:默認top,這里居中顯示,還有bottom android:gray="top" //多行中指針在第一行第一位置 android:capitalize //首字母大寫 android:phoneNumber //輸入電話號碼 android:autoLink=”all” //設置文本超鏈接樣式當點擊網址時,跳向該網址 android:cursorVisible //設定光標為顯示/隱藏,默認顯示。
2)inputType常用設置
android:inputType="textCapCharacters" 字母大寫 android:inputType="textCapWords" 首字母大寫 android:inputType="textCapSentences" 僅第一個字母大寫 android:inputType="textMultiLine" 多行輸入 android:inputType="textPassword" 密碼 android:inputType="number" 數字 android:inputType="numberSigned" 帶符號數字格式 android:inputType="numberDecimal" 帶小數點的浮點格式 android:inputType="datetime" 時間日期 android:inputType="date" 日期鍵盤 android:inputType="time" 時間鍵盤
3)常用的數字輸入設置:
android:numeric="integer" //只可以輸入正整數 android:numeric="decimal" //可以輸入小數 android:numeric="signed" //表示可以輸入整數(正整數或者負整數) android:inputType="numberDecimal" //可以輸入小數,正小數(即只可以加一個小數點的正數) android:maxLength="11" //最多可以輸入11位數字 android:singleLine="true" //單行輸入 android:password="true" //密碼輸入框,可以使得輸入的內容在1秒內變成*字樣 android:inputType="number" //設置只能輸入數字(相當於是輸入正整數),並且默認的彈出框是數字彈出框
4)EditText中,android:maxLines和android:minLines的區別:
例如:
android:maxLines="3" //使用maxLines的EditText最大行數為3行,當輸入的內容超過3行后,它形狀的大小不會根據輸入內容的多少而改變,反正它顯示的內容就是3行
//開始EditText里面沒內容時,默認控件大小為1行
android:minLines="3" //使用minLines的EditText是至它至少顯示3行內容(包括內容為空時)當輸入的內容超過3行后,它形狀的大小根據輸入內容的多少而改變。
5)如何設置輸入框只能輸入指定的字符:
A:在EditText的XML中直接設置:
android:digits="0123456789xyzXYZ#*?" //引號里面輸入你想設置的輸入內容
B:使用Java代碼設置:
EditText.setKeyListener(new NumberKeyListener() { @Override protected char[] getAcceptedChars() { return new char[] { 'a', 'b', '3', '4', '5', '6', '7', '8','9', }; } @Override public int getInputType() { // TODO Auto-generated method stub return android.text.InputType.TYPE_CLASS_PHONE; } });
android 中inputType詳解:
android 1.5以后添加了軟件虛擬鍵盤的功能,所以在輸入提示中將會有對應的軟鍵盤模式。
android中inputType屬性在EditText輸入值時啟動的虛擬鍵盤的風格有着重要的作用。這也大大的方便的操作。有時需要虛擬鍵盤只為字符或只為數字。所以inputType尤為重要。
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"android:inputType="phone" />
//文本類型,多為大寫、小寫和數字符號。
android:inputType="none"
android:inputType="text"
android:inputType="textCapCharacters" 字母大寫
android:inputType="textCapWords" 首字母大寫
android:inputType="textCapSentences" 僅第一個字母大寫
android:inputType="textAutoCorrect" 自動完成
android:inputType="textAutoComplete" 自動完成
android:inputType="textMultiLine" 多行輸入
android:inputType="textImeMultiLine" 輸入法多行(如果支持)
android:inputType="textNoSuggestions" 不提示
android:inputType="textUri" 網址
android:inputType="textEmailAddress" 電子郵件地址
android:inputType="textEmailSubject" 郵件主題
android:inputType="textShortMessage" 短訊
android:inputType="textLongMessage" 長信息
android:inputType="textPersonName" 人名
android:inputType="textPostalAddress" 地址
android:inputType="textPassword" 密碼
android:inputType="textVisiblePassword" 可見密碼
android:inputType="textWebEditText" 作為網頁表單的文本
android:inputType="textFilter" 文本篩選過濾
android:inputType="textPhonetic" 拼音輸入 //數值類型
android:inputType="number" 數字
android:inputType="numberSigned" 帶符號數字格式
android:inputType="numberDecimal" 帶小數點的浮點格式
android:inputType="phone" 撥號鍵盤
android:inputType="datetime" 時間日期
android:inputType="date" 日期鍵盤
android:inputType="time" 時間鍵盤
遇到的一些情景:
一、項目需求是這樣的,如果EditText上面已經被setText()內容,則需要禁止輸入,防止它被修改。
解決:
1、根據判斷條件,設置:
editText.setFocusable(true);或者editText.setFocusable(false);
2、手機軟鍵盤是否可以開啟:
利用 editText.setInputType(InputType.TYPE_NULL);來禁止手機軟鍵盤。
editText.setInputType(InputType.TYPE_CLASS_TEXT);來開啟軟鍵盤。
二、輸入框中,第一位不讓輸入”.“,如何判斷?
if(StringUtils.equals(".", StringUtils.substring(edt.getText().toString(),0,1))){ //截取用戶輸入的內容的第一位,然后跟”.做比較“ Toast.makeText(VisitInfo.this, "里程信息輸入有誤,請重新輸入!",Toast.LENGTH_SHORT).show(); }