Android EditText 屬性修改(光標顏色、粗細,hint)


在xml布局文件中 :
android:textCursorDrawable=”@null” 表示光標的顏色和字體的顏色一樣

當然,我們也可以自定義光標的顏色,在drawable文件夾下寫個shape_cursor_color.xml文件:

1 <?xml version="1.0" encoding="utf-8"?>
2 <shape xmlns:android="http://schemas.android.com/apk/res/android"
3     android:shape="rectangle" >
4 
5     <size android:width="2dp" /> //光標的粗細沒有改變
6 
7     <solid android:color="#ffcf46" />
8 
9 </shape>

注意:<size android:width="2dp" />

光標在已輸入字符串的別的位置都是修改后的粗細 一旦光標移到最后又會變成默認的粗細了.

使用:

1 android:textCursorDrawable=”@drawable/shape_cursor_color”

 

由於默認的hint字體比較丑,我們經常需要自己去設置字體大小和光標樣式:

 1 public static void setHintTextSize(EditText et, String hint, int textSize) {
 2         // 新建一個可以添加屬性的文本對象
 3         SpannableString ss = new SpannableString(hint);
 4         // 新建一個屬性對象,設置文字的大小
 5         AbsoluteSizeSpan ass = new AbsoluteSizeSpan(textSize, true);
 6         // 附加屬性到文本
 7         ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
 8 
 9         // 設置hint
10         et.setHint(new SpannedString(ss)); // 一定要進行轉換,否則屬性會消失
11     }

 

限制輸入框中只能輸入自己定義的這些字符串 如果輸入其它將不予以顯示:

1 android:digits="1234567890.+-*/%\n()"

限制輸入框中只能輸入手機號碼

1 android:phoneNumber="true"

限制輸入框中輸入的任何內容將以"*"符號來顯示

1 android:password="true"
設置只能輸入整數,如果是小數則是:decimal:
1 android:numeric="integer"
以大寫字母寫
1 android:capitalize = "characters"

 

 

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM