欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)EditText添加下劃線

 更新時間:2018年08月24日 16:56:13   作者:星辰之力  
這篇文章主要為大家詳細介紹了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)文章

最新評論