Android基础——自定义EditTExt实现去掉输入框添加下划线
来源:互联网 发布:微信支付 域名备案 编辑:程序博客网 时间:2024/06/06 20:08
使用场景
在实际开发中我们往往需要自定义EditText,去掉输入框,在文本下面添加下划线,在本章中使用自定义TditeText 实现。
实现效果
实现步骤
1、Attributes实现
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="LineEditeText"> <attr name="lineColorEt" format="color"/> </declare-styleable></resources>
2、自定义EditText实现
public class LineEditText extends EditText { private Paint mPaint; private int mLineColor; public LineEditText(Context context) { super(context); } public LineEditText(Context context, AttributeSet attrs) { super(context, attrs); initData(context, attrs); } public LineEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initData(context, attrs); } private void initData(Context context, AttributeSet attrs){ TypedArray attrArrays = context.obtainStyledAttributes(attrs, R.styleable.LineEditeText); mPaint = new Paint(); int lenght = attrArrays.getIndexCount(); for(int i = 0 ; i < lenght; i ++){ int index = attrArrays.getIndex(i); switch (index){ case R.styleable.LineEditeText_lineColorEt: mLineColor = attrArrays.getColor(index,0xFFF); break; } } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint.setStyle(Paint.Style.STROKE); mPaint.setColor(mLineColor); canvas.drawLine(0, getHeight() - 1, getWidth() - 1, getHeight() - 1, mPaint); }}
3、布局文件实现
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:line="http://schemas.android.com/apk/res-auto" android:background="@mipmap/start_bg" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <include android:layout_width="match_parent" android:layout_height="wrap_content" layout="@layout/include_title"/> <com.hervillage.widgets.LineEditText android:paddingBottom="@dimen/login_et_padding_bottom" android:drawablePadding="@dimen/login_et_dw_padding" android:layout_marginTop="@dimen/login_et_margin_top" android:textSize="@dimen/login_et_text_size" line:lineColorEt="@color/start_button_color" android:background="@color/transparent" android:layout_marginRight="@dimen/login_margin_left_right_edit" android:layout_marginLeft="@dimen/login_margin_left_right_edit" android:hint="请输入手机号" android:id="@+id/account_et" android:layout_width="match_parent" android:layout_height="wrap_content" /> <RelativeLayout android:layout_marginTop="@dimen/login_pass_et_margin_top" android:layout_marginRight="@dimen/login_margin_left_right_edit" android:layout_marginLeft="@dimen/login_margin_left_right_edit" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.hervillage.widgets.LineEditText android:paddingBottom="@dimen/login_et_padding_bottom" android:drawablePadding="@dimen/login_et_dw_padding" line:lineColorEt="@color/start_button_color" android:background="@color/transparent" android:textSize="@dimen/login_et_text_size" android:hint="6-16位区分大小写支持符号" android:id="@+id/password_et" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:layout_centerVertical="true" android:layout_alignParentRight="true" android:id="@+id/forget_pass" android:text="忘记密码" android:textColor="@color/start_button_color" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> <Button android:background="@drawable/start_register_button" android:textColor="@color/white" android:layout_marginTop="@dimen/login_button_margin_top" android:text="登 录" android:layout_marginRight="@dimen/login_margin_right" android:layout_marginLeft="@dimen/login_margin_right" android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:textColor="@color/start_button_color" android:layout_marginTop="@dimen/login_button_text_margin_top" android:layout_marginLeft="@dimen/login_margin_right" android:text="新用户注册" android:id="@+id/btn_register" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RelativeLayout android:layout_marginTop="@dimen/login_sf_margin_top" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_marginLeft="@dimen/login_sf_line_tv_left" android:layout_marginRight="@dimen/login_sf_line_tv_right" android:layout_toLeftOf="@+id/line_text" android:layout_centerVertical="true" android:id="@+id/line_left" android:background="@color/start_line_color" android:layout_width="match_parent" android:layout_height="2dp" /> <TextView android:layout_centerHorizontal="true" android:textColor="@color/start_line_color" android:id="@+id/line_text" android:text="使用其他登录方式" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_marginLeft="@dimen/login_sf_line_tv_right" android:layout_marginRight="@dimen/login_sf_line_tv_left" android:layout_toRightOf="@+id/line_text" android:layout_centerVertical="true" android:id="@+id/line_right" android:background="@color/start_line_color" android:layout_width="match_parent" android:layout_height="2dp" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:layout_marginTop="@dimen/sf_top" android:layout_toLeftOf="@+id/sf_xinlang_img" android:layout_marginRight="@dimen/login_margin_sf" android:id="@+id/sf_weixin_img" android:src="@mipmap/sf_weixin_logo" android:layout_width="@dimen/login_sf_image_height_width" android:layout_height="@dimen/login_sf_image_height_width" /> <ImageView android:layout_marginTop="@dimen/sf_top" android:id="@+id/sf_xinlang_img" android:layout_centerHorizontal="true" android:src="@mipmap/sf_xinlang_logo" android:layout_width="@dimen/login_sf_image_height_width" android:layout_height="@dimen/login_sf_image_height_width" /> <ImageView android:layout_marginTop="@dimen/sf_top" android:layout_marginLeft="@dimen/login_margin_sf" android:layout_toRightOf="@+id/sf_xinlang_img" android:id="@+id/sf_qq_img" android:src="@mipmap/sf_qq_logo" android:layout_width="@dimen/login_sf_image_height_width" android:layout_height="@dimen/login_sf_image_height_width" /> </RelativeLayout></LinearLayout>
4、其它属性文件
start_register _button.xml 文件
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><corners android:radius="5dp"/><solid android:color="@color/start_button_color"/></shape>
dimens 文件
<dimen name="login_margin_sf">20dp</dimen><dimen name="login_margin_left_right_edit">40dp</dimen><dimen name="login_margin_right">50dp</dimen><dimen name="login_et_padding_bottom">5dp</dimen><dimen name="login_et_dw_padding">5dp</dimen><dimen name="login_et_margin_top">100dp</dimen><dimen name="login_et_text_size">15sp</dimen><dimen name="login_button_margin_top">30dp</dimen><dimen name="login_button_text_margin_top">10dp</dimen><dimen name="login_sf_margin_top">80dp</dimen><dimen name="login_sf_line_tv_left">20dp</dimen><dimen name="login_sf_line_tv_right">10dp</dimen><dimen name="login_sf_image_height_width">60dp</dimen><dimen name="login_pass_et_margin_top">20dp</dimen>
colors 文件
<color name="start_line_color">#E0004E</color><color name="start_button_color">#D80050</color>
代码不多,整片文章的核心内容主要在onDraw() 方法中,反复体会。
1 0
- Android基础——自定义EditTExt实现去掉输入框添加下划线
- android EditText去掉下划线
- Android 如何自定义EditText 下划线?
- Android中去掉EditText的下划线
- 去掉EditText的下划线
- 关于EditText去掉下划线
- Android--去除EditText边框,添加下划线,
- Android--去除EditText边框,添加下划线
- Android下EditText 添加字体下划线
- 自定义EditText实现IP输入框
- 隐藏EditText输入下划线
- Android 自定义EditText输入框 带清空按钮
- 自定义android EditText输入框光标样式
- Android 自定义EditText输入框 带清空按钮
- Android自定义价格输入框EditText
- 自定义Android Dialog EditText 密码输入框
- 给EditText添加下划线
- 简单去掉EditText的下划线
- warning C4551: function call missing argument list 问题
- FFmpeg中比较重要的函数以及数据结构
- 新的开始
- 无线APP时代HTTP请求DNS优化
- 简单服务器生猛上手
- Android基础——自定义EditTExt实现去掉输入框添加下划线
- 从Samples中入门IOS开发(四)------ 基于socket的网络编程
- c++作业2——模拟ATM机
- leetcode——231—— Power of Two
- JAVA中使用commons-logging和log4j打印日志
- leetcode刷题,总结,记录,备忘78
- EasyDarwin流媒体云平台架构
- https通信加密过程
- leetcode 322. Coin Change-硬币交换|动态规划