閱讀532 返回首頁    go 阿裏雲 go 技術社區[雲棲]


android EditText自定義樣式

1.去掉邊框

EditText的background屬性設置為@null就搞定了:android:background="@null"
style屬性倒是可加可不加

附原文:
@SlumberMachine, that's a great observation! But, it seems that there is more to making a TextView editable than just setting android:editable="true". It has to do with the "input method" - what ever that is - and that is where the real difference between TextView and EditText lies. TextView was designed with an EditText in mind, that's for sure. One would have to look at the EditText source code and probably EditText style to see what's really going on there. Documentation is simply not enough.

I have asked the same question back at android-developers group, and got a satisfactory answer. This is what you have to do:

XML:
<EditText android: android:layout_width="fill_parent"
     
     android:background="@null" android:textColor="@null"/>


Instead of you can also write _blank" href="https://my.oschina.net/asia" >@android:style/Widget.TextView", don't ask me why and what it means.

2.Android EditText 改變邊框顏色

第一步:為了更好的比較,準備兩個一模一樣的EditText(當Activity啟動時,焦點會在第一個EditText上,如果你不希望這樣隻需要寫一個高度和寬帶為0的EditText即可避免,這裏就不這麼做了),代碼如下:

 

[html] view plaincopy
  1. <EditText   
  2.     android:layout_width="fill_parent"  
  3.         android:layout_height="36dip"  
  4.         android:background="@drawable/bg_edittext"  
  5.         android:padding="5dip"  
  6.     android:layout_margin="36dip"  
  7.     android:textColorHint="#AAAAAA"  
  8.     android:textSize="15dip"  
  9.     android:singleLine="true"  
  10.     android:hint="請輸入..."  
  11. />  


接下來建立三個xml文件,分別為輸入框未獲得焦點時的背景,輸入框獲得焦點時的背景,selector背景選擇器(這裏能獲得輸入框什麼時候獲得和失去焦點),代碼如下:

bg_edittext_normal.xml(未獲得焦點時)

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <shape xmlns:android="https://schemas.android.com/apk/res/android">   
  3.     <solid android:color="#FFFFFF" />   
  4.     <corners android:radius="3dip"/>  
  5.     <stroke    
  6.         android:width="1dip"    
  7.         android:color="#BDC7D8" />   
  8. </shape>  

bg_edittext_focused.xml(獲得焦點時)

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <shape xmlns:android="https://schemas.android.com/apk/res/android">   
  3.     <solid android:color="#FFFFFF" />   
  4.     <corners android:radius="3dip"/>  
  5.     <stroke    
  6.         android:width="1dip"    
  7.         android:color="#728ea3" />   
  8. </shape>  

bg_edittext.xml(selector選擇器,這方麵資料網上很多)

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <selector xmlns:android="https://schemas.android.com/apk/res/android">  
  3.         <item android:state_window_focused="false" android:drawable="@drawable/contact_edit_edittext_normal" />  
  4.        <item android:state_focused="true" android:drawable="@drawable/contact_edit_edittext_focused" />  
  5. </selector>  


這樣就OK了,效果圖如下:

第二個輸入框邊框變為深色,是不是這樣更友好點。



最後更新:2017-04-03 12:56:32

  上一篇:go NYOJ58-最少步數
  下一篇:go android EditText 設置密碼鎖源碼