1. 程式人生 > >Android限定EditText的輸入型別為數字或者英文(包括大小寫)

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

 // 監聽密碼輸入框的輸入內容型別,不可以輸入中文
    TextWatcher mTextWatcher = 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) { 
String temp = edt.toString();
if(edt.toString().getBytes().length != edt.length()){
edt.delete(temp.length()-1, temp.length());
}
//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) {
//try {
//throw new Exception("登入頁面監聽密碼輸入框只能輸入數字或者英文出錯");
//} catch (Exception e1) { 
//e1.printStackTrace();
//}
//}
}
};

其實有兩種方案:

1.在xml:EditText 設定屬性----
android:digis="ABCDE555555&&&&&"
ABCDE555555&&&&&"是你的限制規則。
例如:android:digits="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
規則是隻能輸入英文字母(小寫)和數字

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:drawableBottom在text的下方輸出一個drawable,如圖片。如果指定一個顏色的話會把text的背景設為該顏色,並且同時和background使用時覆蓋後者。

android:drawableLeft在text的左邊輸出一個drawable,如圖片。

android:drawablePadding設定text與drawable(圖片)的間隔,與drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可設定為負數,單獨使用沒有效果。

android:drawableRight在text的右邊輸出一個drawable,如圖片。

android:drawableTop在text的正上方輸出一個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:marqueeRepeatLimit在ellipsize指定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

本公眾號將以推送Android各種技術乾貨或碎片化知識,以及整理老司機日常工作中踩過的坑涉及到的經驗知識為主,也會不定期將正在學習使用的新技術總結出來進行分享。每天一點乾貨小知識把你的碎片時間充分利用起來。

本公眾號將以推送Android各種技術乾貨或碎片化知識,以及整理老司機日常工作中踩過的坑涉及到的經驗知識為主,也會不定期將正在學習使用的新技術總結出來進行分享。每天一點乾貨小知識把你的碎片時間充分利用起來。