自定义组合控件控制EditText输入类型

自定义属性值,这个主要是在res/values/attrs.xml文件中进行自定义:

<declare-styleable name ="LineEditText">               
   <attr name = "textMaxLength" format = "integer"/>  
   <attr name="textInputType" format="enum">  
        <enum name="phone" value="0" />  
        <enum name="number" value="1" />  
        <enum name="textMutiLine" value="2" />  
        </attr>  
</declare-styleable>  

自定义组合控件的布局文件:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:orientation="vertical" >  
    <EditText  
        android:id="@+id/et_text"  
        android:textColor="#332E38"   
        android:textSize="13sp"  
        android:background="@null"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"/>  
     <View   
         android:layout_width="match_parent"   
         android:layout_height="0.5dp"   
         android:background="@android:color/black"/>  
</LinearLayout> 

自定义View类:

//定义一个类继承自根布局  
public class LineEditText extends LinearLayout{  

    private static final int TYPE_DEFAUT = 3;  
    private Paint paint;  
    private EditText et_text;    
    int maxLength;
    int inputType;

    //布局中使用,只需要重写带两个、三个参数的构造函数即可,最终是调用三个参数的构造  
    public LineEditText(Context context, AttributeSet attrs) {  
        this(context, attrs,0);  
    }  

    public LineEditText(Context context, AttributeSet attrs, int defStyleAttr) {  
        super(context, attrs, defStyleAttr);  
        initAttrs(context,attrs); 
        initViews(context,attrs); 
    }  

    private initAttrs(Context context,AttributeSet attrs) {
        //获取并设置属性值  
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LineEditText); 
        //输入最大字符属性  
        maxLength = array.getInt(R.styleable.LineEditText_textMaxLength,-1);  
        //输入类型属性  
        inputType = array.getInt(R.styleable.LineEditText_textInputType, TYPE_DEFAUT);  
        //注意要回收  
        array.recycle();  
    }

    private void initViews(Context context,AttributeSet attrs) {  
        //将布局与创建的类关联起来,注意第三个参数是this,而不是null,因为是要填充到当前容器中  
         View view = View.inflate(context, R.layout.view_lineedittext, this);  
         et_text = (EditText) view.findViewById(R.id.et_text); 

        //设置最大字符属性,这个可以去看下TextView的源码就知道为什么这么写了  
        if (maxLength >= 0) {  
            et_text.setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxLength) });  
        }else{  
            et_text.setFilters(new InputFilter[0]);  
        }  

        //设置输入类型控制  
        switch (inputType) {  
        case TYPE_DEFAUT:  
            et_text.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);  //多行文本  
            //要使多行文本属性生效要加上下面两句才行  
            et_text.setSingleLine(false);             
            et_text.setHorizontallyScrolling(false);   
            break;  
        case 0:  
            et_text.setInputType(InputType.TYPE_CLASS_PHONE);       //电话号码  
            break;  
        case 1:  
            et_text.setInputType(InputType.TYPE_CLASS_NUMBER);      //数字  
            break;    
        case 2:  
            et_text.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);  
            et_text.setSingleLine(false);  
            et_text.setHorizontallyScrolling(false);   
            break;  
        default:  
            et_text.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);  
            et_text.setSingleLine(false);  
            et_text.setHorizontallyScrolling(false);   
            break;  
        }  

    }  
}

接着就可以正常使用这个自定义控件了

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <com.zcn.view.LineEditText
        android:id="@+id/inputbox_stulogin_number"
        app:textMaxLength="11"
        app:textInputType="textMutiLine"/>
</LinearLayout>

文章参考:http://blog.csdn.net/zxhandroid/article/details/72835133

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值