本文實例講述了Android EditText自定義樣式的方法。分享給大家供大家參考,具體如下:
1.去掉邊框
EditText的background屬性設(shè)置為@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:id="@+id/title" android:layout_width="fill_parent" style="?android:attr/textViewStyle" android:background="@null" android:textColor="@null"/>
Instead of style="?android:attr/textViewStyle" you can also write style="@android:style/Widget.TextView", don't ask me why and what it means.
2.Android EditText 改變邊框顏色
第一步:為了更好的比較,準備兩個一模一樣的EditText(當(dāng)Activity啟動時,焦點會在第一個EditText上,如果你不希望這樣只需要寫一個高度和寬帶為0的EditText即可避免,這里就不這么做了),代碼如下:
<EditText android:layout_width="fill_parent" android:layout_height="36dip" android:background="@drawable/bg_edittext" android:padding="5dip" android:layout_margin="36dip" android:textColorHint="#AAAAAA" android:textSize="15dip" android:singleLine="true" android:hint="請輸入..."/>
接下來建立三個xml文件,分別為輸入框未獲得焦點時的背景,輸入框獲得焦點時的背景,selector背景選擇器(這里能獲得輸入框什么時候獲得和失去焦點),代碼如下:
bg_edittext_normal.xml(未獲得焦點時)
<?xml version="1.0" encoding="UTF-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFF" /> <corners android:radius="3dip"/> <stroke android:width="1dip" android:color="#BDC7D8" /></shape>
bg_edittext_focused.xml(獲得焦點時)
<?xml version="1.0" encoding="UTF-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFF" /> <corners android:radius="3dip"/> <stroke android:width="1dip" android:color="#728ea3" /></shape>
bg_edittext.xml(selector選擇器,這方面資料網(wǎng)上很多)
<?xml version="1.0" encoding="UTF-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_window_focused="false" android:drawable="@drawable/contact_edit_edittext_normal" /> <item android:state_focused="true" android:drawable="@drawable/contact_edit_edittext_focused" /></selector>
這樣就OK了,效果圖如下:

第二個輸入框邊框變?yōu)樯钌遣皇沁@樣更友好點。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android通信方式總結(jié)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
新聞熱點
疑難解答
圖片精選