EditText自定义边框背景与动态检测用户输入
来源:互联网 发布:阿里云com续费 编辑:程序博客网 时间:2024/05/24 22:43
一、EditText自定义边框背景
1.效果演示
2.代码实现
(1)res/drawable/shape_edit_normal.xml
功能:编辑框没获得焦点时,使用该shape。<shape.../>为根元素的ShapeDrawable资源,主要用于定义一个基本的几何图形,如矩形、圆形、线条等。
<solid.../>子元素用于指定填充集合图形的的颜色;
<corners.../>子元素用于定义几个图形的四个角的弧度;
<gradient../>子元素用于指定填充几何图形的渐变颜色;
<stroke../>子元素指定几何图形边框线宽度以及颜色;
<padding../>子元素指定几何图形的内边框
源码如下:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFF" /> <corners android:radius="4dip"/> <stroke android:width="1dip" android:color="#BDC7D8" /> </shape>
(2)res/drawable/shape_edit_focus.xml
源码如下:编辑框获得焦点时,使用该shape。与上面的shape区别是,<stroke../>元素的颜色属性值设置不一致。
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFF" /> <corners android:radius="4dip"/> <stroke android:width="1dip" android:color="#728ea3" /> </shape>
(3)res/drawable/selector_edit_frame.xml
功能:用于设置文件编辑框是否有获取焦点时对应的<shape.../>资源。<selector.../>为根元素的StateListDrawable资源,用于组织多个Drawable对象。当使用StateListDrawable作为目标组件的背景、前景图片时,StateListDrawable对象所显示的Drawable对象会随目标组件状态的改变自动切换。该元素可以包含多个<item../>子元素,该元素可指定如下属性:
>android:color或android:drawable:指定颜色或Drawable对象;
>android:state_xxx:指定一个特定状态;
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_window_focused="false" android:drawable="@drawable/shape_edit_normal"/> <item android:state_focused="true" android:drawable="@drawable/shape_edit_focus"/></selector>
(4)res/layout/main.xml
...... <EditText android:background="drawable/selector_edit_frame" android:layout_width="match_parent" android:layout_weight="wrap_content"/> ......
二、动态检测EditText的输入
在Android项目开发中,往往我们在实现登录或注册功能时,需要动态来检测验证码框中的内容来决定是否使能登录/注册按钮。Android系统提供了一个TextWatch事件监听器,其可以实现动态检测EditText输入情况。
1.效果演示
2.源码实现
功能:动态检测验证码输入框是否与随机验证码字符匹配,若匹配则使能登录按钮并改变其背景颜色。
protected void onCreate(Bundle savedInstanceState) { Button loginBtn = (Button)findViewById(R.id.login); EditText secCode = (EditText) findViewById(R.id.login_security_code); secCode.addTextChangedListener(new TextWatcher() { /** *文本编辑框内容未改变前回调该方法 */ public void beforeTextChanged(CharSequence s, int start, int count, int after) { loginBtn.setEnabled(false); // loginBtn.setBackgroundColor(Color.parseColor("#DEB887")); } /** *文本编辑框内容改变时回调该方法 */ public void onTextChanged(CharSequence s, int start, int before, int count) { loginBtn.setEnabled(false); loginBtn.setBackgroundColor(Color.parseColor("#DEB887")); } /** *文本编辑框内容改变后回调该方法 */ public void afterTextChanged(Editable s) { if (secCode.getText().toString().equalsIgnoreCase(verityCode)) { loginBtn.setEnabled(true); loginBtn.setBackgroundResource(R.drawable.selector_btn_background); } } });}
1 0
- EditText自定义边框背景与动态检测用户输入
- android EditText 自定义边框 背景
- 自定义android控件EditText 自定义边框 背景
- 自定义android控件EditText 自定义边框 背景
- 自定义android控件EditText边框背景
- 转,EditText自定义边框和背景
- 自定义android控件EditText边框背景
- 使用图片自定义android控件EditText 边框背景
- android editText 自定义边框
- android 自定义edittext边框
- Edittext自定义边框颜色
- 自定义edittext边框
- 自定义edittext边框
- 自定义背景边框
- 一个EditText,实现无边框,无背景,输入提示图标高亮闪动。
- Android EditText,实现无边框,无背景,输入提示图标高亮闪动
- EditText动态更改背景
- EditText之自定义边框颜色
- 关于设置半透明背景的一些技巧
- 【j2ee spring】35、巴巴运动网的产品类别管理(2)
- 深度学习笔记8 数据预处理
- Android点击EditText文本框之外任何地方隐藏键盘的解决办法
- vs编写ActiveX控件,无法运行调试配置
- EditText自定义边框背景与动态检测用户输入
- JAVA JDK下载以及环境变量设置
- HDU 5238 Calculator(中国剩余定理+线段树)
- Delphi 各版本下载
- 城市_我们只是过客
- 开始。。。
- 华尔街人必读40本金融佳作
- 手机屏幕旋转180度
- HBuild+Mui学习笔记(二):一些知识点