Android EditText 改變邊框顏色

2021-06-26 19:07:29 字數 2311 閱讀 7417

**:

第一步:為了更好的比較,準備兩個一模一樣的edittext(當activity啟動時,焦點會在第乙個edittext上,如果你不希望這樣只需要寫乙個高度和寬頻為0的edittext即可避免,這裡就不這麼做了),**如下:

[html]view plain

copy

<

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(未獲得焦點時)

[html]view plain

copy

xmlversion

="1.0"

encoding

="utf-8"

?>

<

shape

xmlns:android

="">

<

solid

android:color

="#ffffff"

/>

<

corners

android:radius

="3dip"

/>

<

stroke

android:width

="1dip"

android:color

="#bdc7d8"

/>

shape

>

bg_edittext_focused.xml(獲得焦點時)

[html]view plain

copy

xmlversion

="1.0"

encoding

="utf-8"

?>

<

shape

xmlns:android

="">

<

solid

android:color

="#ffffff"

/>

<

corners

android:radius

="3dip"

/>

<

stroke

android:width

="1dip"

android:color

="#728ea3"

/>

shape

>

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

[html]view plain

copy

xmlversion

="1.0"

encoding

="utf-8"

?>

<

selector

xmlns: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了,效果圖如下:

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

android edittext學習記錄

實現在edittext中輸入自定義表情,需要將放在drawable中的表情,嵌入到edittext中,這裡利用imagespan實現該功能。第一步 利用反射機制,得到資源的id,生成bitmap物件 field field r.drawable.class.getdeclaredfield imag...

Android EditText屬性總結

接下來老於就總結一下 edittext屬性吧!android inputtype none 這個就不要解釋了吧 android inputtype text 文字型別,多為大寫 小寫和數字符號 android inputtype textcapcharacters 字母大寫 android inpu...

Android EditText左右滑動

預設的edittext寫滿一行後會自動轉換到下一行,但是我們有時候需要的滿一行後左右滑動過去而不是換行,此時我們可以設定edittext 的屬性 android maxlines 1 即可,如果不可以,則加上 android singleline true android layout width ...