Android實現(xiàn)EditText添加下劃線
在安卓高版本,默認是有下劃線的,其默認下劃線的顏色是由其主題顏色來控制的!
控制如下:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> **<item name="colorAccent">@color/colorPrimaryDark</item>**
所以,只需要修改colorAccent的顏色,其下劃線的顏色既可以修改!
在低版本和高版本中,同樣是可以去添加下劃線的!方法有二:
方法一:
//此時必須要設(shè)置其背景為空 <EditText android:background="@null" android:drawableBottom="@drawable/line" android:hint="請輸入您的手機號碼" android:layout_width="match_parent" android:layout_height="wrap_content"/>
//資源名稱為 drawable/line <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/colorBlue" /> <size android:height="1dp" android:width="1000dp" /> </shape>
方法二:通過自定義editText
public class UnderLineEditText extends EditText { private Paint paint; public UnderLineEditText(Context context, AttributeSet attrs) { super(context, attrs); //設(shè)置畫筆的屬性 paint = new Paint(); paint.setStyle(Paint.Style.STROKE); //設(shè)置畫筆顏色為紅色 paint.setColor(Color.RED); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); /**canvas畫直線,從左下角到右下角,this.getHeight()-2是獲得父edittext的高度,但是必須要-2這樣才能保證 * 畫的橫線在edittext上面,和原來的下劃線的重合 */ canvas.drawLine(0, this.getHeight()-2, this.getWidth()-2, this.getHeight()-2, paint); } }
這里有幾點需要注意:
其一:也可以繼承android.support.v7.widget.AppCompatEditText,但是有時會出現(xiàn)獲取不到焦點的現(xiàn)狀
其二:下劃線的的位置確定
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談Android為RecyclerView增加監(jiān)聽以及數(shù)據(jù)混亂的小坑
下面小編就為大家?guī)硪黄獪\談Android為RecyclerView增加監(jiān)聽以及數(shù)據(jù)混亂的小坑。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04詳解Android ContentProvider的基本原理和使用
ContentProvider(內(nèi)容提供者)是 Android 的四大組件之一,管理 Android 以結(jié)構(gòu)化方式存放的數(shù)據(jù),以相對安全的方式封裝數(shù)據(jù)(表)并且提供簡易的處理機制和統(tǒng)一的訪問接口供其他程序調(diào)用2021-06-06Android自定義view之利用drawArc方法實現(xiàn)動態(tài)效果(思路詳解)
這篇文章主要介紹了Android自定義view之利用drawArc方法實現(xiàn)動態(tài)效果,drawArc方法包含了五個參數(shù),具體細節(jié)在本文中給大家提到過,需要的朋友可以參考下2021-08-08Android?配合Mat工具監(jiān)聽查找內(nèi)存泄漏的操作方法
這篇文章主要介紹了Android?配合Mat工具監(jiān)聽查找內(nèi)存泄漏問題,使用Android Studio Profiler查看內(nèi)存的操作,本文通過圖文實例相結(jié)合給大家介紹的非常詳細,需要的朋友可以參考下2022-05-05Android網(wǎng)格布局GridView實現(xiàn)漂亮的多選效果
這篇文章主要為大家詳細介紹了Android網(wǎng)格布局GridView實現(xiàn)漂亮的多選效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12Android Studio開發(fā)之 JNI 篇的簡單示例
本篇文章主要介紹了Android Studio開發(fā)之 JNI 篇的簡單示例,它提供了若干的API實現(xiàn)了Java和其他語言的通信,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10