欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

Android实现EditText添加下划线,

来源: 开发者 投稿于  被查看 1604 次 评论:126

Android实现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,但是有时会出现获取不到焦点的现状

其二:下划线的的位置确定

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持3672js教程。

您可能感兴趣的文章:
  • Android 登录页面的实现代码(密码显示隐藏、EditText 图标切换、限制输入长度)
  • Android EditText随输入法一起移动并悬浮在输入法之上的示例代码
  • Android EditText每4位自动添加空格效果
  • Android EditText追加空格、限制字符等方法示例
  • Android EditText长按菜单中分享功能的隐藏方法
  • Android中多个EditText输入效果的解决方式
  • Android实现微信朋友圈评论EditText效果
  • Android EditText监听回车键并处理两次回调问题
  • Android自定义密码输入EditTextLayout
  • Android EditText限制输入整数和小数的位数的方法示例
  • Android使用EditText小技巧汇总

用户评论