码迷,mamicode.com
首页 > 其他好文 > 详细

EditText自定义边框背景与动态检测用户输入

时间:2015-06-17 11:43:39      阅读:607      评论:0      收藏:0      [点我收藏+]

标签:android   应用   

一、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);
            }
       }
  });
}    

EditText自定义边框背景与动态检测用户输入

标签:android   应用   

原文地址:http://blog.csdn.net/u012637501/article/details/46529865

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!