Android限定EditText的輸入類型爲數字或者英文(包括大小寫)

其實有兩種方案:

1.在xml:EditText 設置屬性----
android:digis="ABCDE555555&&&&&" (尼瑪CSDN也坑爹啊, 根本不是digis, 我還奇怪怎麼冒個digis 出來,  經個人驗證是digits !! 尼瑪原作者寫錯就算了, 你們這些到處亂轉載騙點擊率的傢伙多少檢查下原文啊!!! )
ABCDE555555&&&&&"是你的限制規則。
例如:android:digits="0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
規則是隻能輸入英文字母(小寫)和數字


2.EditText,TextView只能輸入字母加數字,可在View空間後面加上監聽器,如下

tvPassword.addTextChangedListener(new TextWatcher() {

@Override

public void onTextChanged(CharSequence s, int start, int before, int count) {

}

 

@Override

public void beforeTextChanged(CharSequence s, int start, int count,

int after) {

}

 

@Override

public void afterTextChanged(Editable edt) {

try {

 String temp = edt.toString();

        String tem = temp.substring(temp.length()-1, temp.length());

char[] temC = tem.toCharArray();

int mid = temC[0];

if(mid>=48&&mid<=57){//數字

return;

}

if(mid>=65&&mid<=90){//大寫字母

return;

}

if(mid>97&&mid<=122){//小寫字母

return;

}

edt.delete(temp.length()-1, temp.length());

} catch (Exception e) {

// TODO: handle exception

}

}

});

 

 

 

1.EditText,TextView只能輸入兩位小數,先在XML文件里加上輸入性:android:numeric="integer"//設置只能輸入整數,如果是小數則是:decimal

然後在View空間後面加上監聽器,如下

EditText txtInput = (EditText) findViewById(R.id.txtInput);

txtInput.addTextChangedListener(new TextWatcher() 

{

    public void afterTextChanged(Editable edt) 

    {

        String temp = edt.toString();

        int posDot = temp.indexOf(".");

        if (posDot <= 0) return;

        if (temp.length() - posDot - 1 > 2)

        {

            edt.delete(posDot + 3, posDot + 4);

        }

    }

 

    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

 

    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

});

 

EditText屬性描述

android:layout_gravity="center_vertical"//設置控件顯示的位置:默認top,這裏居中顯示,還有bottom

android:hint="請輸入數字!"//設置顯示在空間上的提示信息

android:numeric="integer"//設置只能輸入整數,如果是小數則是:decimal

android:maxLength="8"  //限制輸入長度爲8

android:singleLine="true"//設置單行輸入,一旦設置爲true,則文字不會自動換行。

android:gray="top" //多行中指針在第一行第一位置et.setSelection(et.length());//調整光標到最後一行

android:autoText //自動拼寫幫助

android:capitalize //首字母大寫

android:digits //設置只接受某些數字

android:singleLine //是否單行或者多行,回車是離開文本框還是文本框增加新行

android:numeric //只接受數字

android:password //密碼

android:phoneNumber // 輸入電話號碼

android:editable //是否可編輯

android:autoLink=all” //設置文本超鏈接樣式當點擊網址時,跳向該網址

android:password="true"//設置只能輸入密碼

android:textColor = "#ff8c00"//字體顏色

android:textStyle="bold"//字體,bold, italic, bolditalic

android:textSize="20dip"//大小

android:capitalize = "characters"//以大寫字母寫

android:textAlign="center"//EditText沒有這個屬性,但TextView

android:textColorHighlight="#cccccc"//被選中文字的底色,默認爲藍色

android:textColorHint="#ffff00"//設置提示信息文字的顏色,默認爲灰色

android:textScaleX="1.5"//控制字與字之間的間距

android:typeface="monospace"//字型,normal, sans, serif, monospace

android:background="@null"//空間背景,這裏沒有,指透明

android:layout_weight="1"//權重 在控制控   件顯示的大小時蠻有用的。

android:textAppearance="?android:attr/textAppearanceLargeInverse"//文字外觀,這裏引用的是系統自帶的一個外觀,?表示系統是否有這種外觀,否則使用默認的外觀。

TextView屬性名稱描述

android:autoLink設置是否當文本爲URL鏈接/email/電話號碼/map時,文本顯示爲可點擊的鏈接。可選值(none/web/email/phone/map/all)

android:autoText如果設置,將自動執行輸入值的拼寫糾正。此處無效果,在顯示輸入法並輸入的時候起作用。

android:bufferType指定getText()方式取得的文本類別。選項editable 類似於StringBuilder可追加字符,也就是說getText後可調用append方法設置文本內容。spannable 則可在給定的字符區域使用樣式,參見這裏1、這裏2

android:capitalize設置英文字母大寫類型。此處無效果,需要彈出輸入法才能看得到,參見EditView此屬性說明。

android:cursorVisible設定光標爲顯示/隱藏,默認顯示。

android:digits設置允許輸入哪些字符。如“1234567890.+-*/% ()”

android:drawableBottomtext的下方輸出一個drawable,如圖片。如果指定一個顏色的話會把text的背景設爲該顏色,並且同時和background使用時覆蓋後者。

android:drawableLefttext的左邊輸出一個drawable,如圖片。

android:drawablePadding設置textdrawable(圖片)的間隔,與drawableLeftdrawableRightdrawableTopdrawableBottom一起使用,可設置爲負數,單獨使用沒有效果。

android:drawableRighttext的右邊輸出一個drawable,如圖片。

android:drawableToptext的正上方輸出一個drawable,如圖片。

android:editable設置是否可編輯。這裏無效果,參見EditView

android:editorExtras設置文本的額外的輸入數據。在EditView再討論。

android:ellipsize設置當文字過長時,該控件該如何顯示。有如下值設置:”start”—?省略號顯示在開頭;”end”——省略號顯示在結尾;”middle”—-省略號顯示在中間;”marquee” ——以跑馬燈的方式顯示(動畫橫向移動)

android:freezesText設置保存文本的內容以及光標的位置。參見:這裏。

android:gravity設置文本位置,如設置成“center”,文本將居中顯示。

android:hintText爲空時顯示的文字提示信息,可通過textColorHint設置提示信息的顏色。此屬性在EditView中使用,但是這裏也可以用。

android:imeOptions附加功能,設置右下角IME動作與編輯框相關的動作,如actionDone右下角將顯示一個完成,而不設置默認是一個回車符號。這個在EditView中再詳細說明,此處無用。

android:imeActionId設置IME動作ID。在EditView再做說明,可以先看這篇帖子:這裏。

android:imeActionLabel設置IME動作標籤。在EditView再做說明。

android:includeFontPadding設置文本是否包含頂部和底部額外空白,默認爲true

android:inputMethod爲文本指定輸入法,需要完全限定名(完整的包名)。例如:com.google.android.inputmethod.pinyin,但是這裏報錯找不到。

android:inputType設置文本的類型,用於幫助輸入法顯示合適的鍵盤類型。在EditView中再詳細說明,這裏無效果。

android:linksClickable設置鏈接是否點擊連接,即使設置了autoLink

android:marqueeRepeatLimitellipsize指定marquee的情況下,設置重複滾動的次數,當設置爲marquee_forever時表示無限次。

android:ems設置TextView的寬度爲N個字符的寬度。這裏測試爲一個漢字字符寬度,如圖:

android:maxEms設置TextView的寬度爲最長爲N個字符的寬度。與ems同時使用時覆蓋ems選項。

android:minEms設置TextView的寬度爲最短爲N個字符的寬度。與ems同時使用時覆蓋ems選項。

android:maxLength限制顯示的文本長度,超出部分不顯示。

android:lines設置文本的行數,設置兩行就顯示兩行,即使第二行沒有數據。

android:maxLines設置文本的最大顯示行數,與width或者layout_width結合使用,超出部分自動換行,超出行數將不顯示。

android:minLines設置文本的最小行數,與lines類似。

android:lineSpacingExtra設置行間距。

android:lineSpacingMultiplier設置行間距的倍數。如”1.2”

android:numeric如果被設置,該TextView有一個數字輸入法。此處無用,設置後唯一效果是TextView有點擊效果,此屬性在EdtiView將詳細說明。

android:password以小點”.”顯示文本

android:phoneNumber設置爲電話號碼的輸入方式。

android:privateImeOptions設置輸入法選項,此處無用,在EditText將進一步討論。

android:scrollHorizontally設置文本超出TextView的寬度的情況下,是否出現橫拉條。

android:selectAllOnFocus如果文本是可選擇的,讓他獲取焦點而不是將光標移動爲文本的開始位置或者末尾位置。TextView中設置後無效果。

android:shadowColor指定文本陰影的顏色,需要與shadowRadius一起使用。效果:

android:shadowDx設置陰影橫向座標開始位置。

android:shadowDy設置陰影縱向座標開始位置。

android:shadowRadius設置陰影的半徑。設置爲0.1就變成字體的顏色了,一般設置爲3.0的效果比較好。

android:singleLine設置單行顯示。如果和layout_width一起使用,當文本不能全部顯示時,後面用“…”來表示。如android:text="test_ singleLine " android:singleLine="true" android:layout_width="20dp"將只顯示“t…”。如果不設置singleLine或者設置爲false,文本將自動換行

android:text設置顯示文本.

android:shadowDx設置陰影橫向座標開始位置。

android:shadowDy設置陰影縱向座標開始位置。

android:shadowRadius設置陰影的半徑。設置爲0.1就變成字體的顏色了,一般設置爲3.0的效果比較好。

android:singleLine設置單行顯示。如果和layout_width一起使用,當文本不能全部顯示時,後面用“…”來表示。如android:text="test_ singleLine " android:singleLine="true" android:layout_width="20dp"將只顯示“t…”。如果不設置singleLine或者設置爲false,文本將自動換行

android:text設置顯示文本.

android:textSize設置文字大小,推薦度量單位”sp”,如”15sp”

android:textStyle設置字形[bold(粗體) 0, italic(斜體) 1, bolditalic(又粗又斜) 2] 可以設置一個或多個,用“|”隔開

android:typeface設置文本字體,必須是以下常量值之一:normal 0, sans 1, serif 2, monospace(等寬字體) 3]

android:height設置文本區域的高度,支持度量單位:px(像素)/dp/sp/in/mm(毫米)

android:maxHeight設置文本區域的最大高度

android:minHeight設置文本區域的最小高度

android:width設置文本區域的寬度,支持度量單位:px(像素)/dp/sp/in/mm(毫米),與layout_width的區別看這裏。

android:maxWidth設置文本區域的最大寬度

android:minWidth設置文本區域的最小寬度

android:textAppearance設置文字外觀。如“?android:attr/textAppearanceLargeInverse”這裏引用的是系統自帶的一個外觀,?表示系統是否有這種外觀,否則使用默認的外觀。

可設置的值如下:

textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmall/textAppearanceSmallInverse

android:textAppearance設置文字外觀。如“?android:attr/textAppearanceLargeInverse”這裏引用的是系統自帶的一個外觀,?表示系統是否有這種外觀,否則使用默認的外觀。

可設置的值如下:

textAppearanceButton、textAppearanceInverse、textAppearanceLarge、

textAppearanceLargeInverse、textAppearanceMedium、textAppearanceMediumInverse

發佈了65 篇原創文章 · 獲贊 26 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章