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的顏色,其下劃線的顏色既可以修改!
在低版本和高版本中,同樣是可以去添加下劃線的!方法有二:
方法一:
//此時必須要設置其背景為空
<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);
//設置畫筆的屬性
paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
//設置畫筆顏色為紅色
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)狀
其二:下劃線的的位置確定
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
詳解Android如何設計一個全局可調用的ViewModel對象
很多時候我們需要維護一個全局可用的ViewModel,因為這樣可以維護全局同一份數(shù)據(jù)源,且方便使用協(xié)程綁定App的生命周期,那如何設計全局可用的ViewModel對象,文中介紹的非常詳細,需要的朋友可以參考下2023-05-05
Android中SparseArray性能優(yōu)化的使用方法
這篇文章主要為大家詳細介紹了Android中SparseArray性能優(yōu)化的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-04-04
Android自定義PasswordInputView密碼輸入
這篇文章主要為大家詳細介紹了Android自定義PasswordInputView密碼輸入功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08
Android應用中使用XmlSerializer序列化XML數(shù)據(jù)的教程
這篇文章主要介紹了Android應用中使用XmlSerializer序列化XML數(shù)據(jù)的教程,XmlSerializer序列化XML同時也是將數(shù)據(jù)寫為XML格式的基本方法,需要的朋友可以參考下2016-04-04
Android自定義帶增長動畫和點擊彈窗提示效果的柱狀圖DEMO
這篇文章主要介紹了Android自定義帶增長動畫和點擊彈窗提示效果的柱狀圖的相關資料,非常不錯具有一定的參考借鑒價值,需要的朋友可以參考下2016-11-11
Android畫板開發(fā)之添加背景和保存畫板內容為圖片
這篇文章主要為大家詳細介紹了Android畫板開發(fā)之添加背景和保存畫板內容為圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12

