自定义的MyEditText,自带最多输入多少字符功能

自定义的控件,MyEditText。自带最多输入多少字符的功能

这里写图片描述
废话不多少,直接上代码吧

/** 
 * @author  张玉水 
 */
public class MyEditText extends RelativeLayout {

    //private String NAMESPACE="http://schemas.android.com/apk/res/com.babyrun.mmsh";
    private int maxLength=200;
    private  int minLines=5;
    private  Context mContext;
    private EditText et;
    private TextView tv;

    public MyEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext=context;
        //下边是引入域名空间用到代码段,在studio中,不能像上边的NAMESPACE一样引入命名空间,所以
        //用setCustomAttributes()方法得到自定义的属性
//      if (attrs!=null) {
//          maxLength = attrs.getAttributeIntValue(NAMESPACE, "maxlength", 200);
//          minLines = attrs.getAttributeIntValue(NAMESPACE, "minlines", 5);
//      }
        if (attrs!=null) {
            setCustomAttributes(attrs);
        }

        init(context);
    }

    public MyEditText(Context context, AttributeSet attrs) {
        this(context, attrs,0);
        this.mContext=context;

    }

    public MyEditText(Context context,int maxlenth) {
        super(context);
        this.mContext=context;
        maxLength=maxlenth; 
        init(context);

    }

    private void setCustomAttributes(AttributeSet attrs) {
        TypedArray a = mContext.obtainStyledAttributes(attrs,R.styleable.MyEditText);
        //最大长度
        maxLength = a.getInt(R.styleable.MyEditText_maxlength,200);
        //最小高度
        minLines = a.getInt(R.styleable.MyEditText_minlines, 5);

    }

    public void hint(String s){
        et.setHint(s);
    }
    //初始化
    private void init(Context context) {
        View view = View.inflate(context, R.layout.myedittext, null);
        et = (EditText) view.findViewById(R.id.et);
        et.setMinLines(minLines);
       // et.setInputType(InputType.TYPE_CLASS_TEXT); //输入类型  
        et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)}); //最大输入长度 
        tv = (TextView) view.findViewById(R.id.tv);
        int length = et.getText().toString().length();
        tv.setText(length+"/"+maxLength);
        et.addTextChangedListener(new TextWatcher() {


            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int length = et.getText().toString().length();
                tv.setText(length+"/"+maxLength);
            }
        });

        addView(view);

    }
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
    //实现类似于Edittext的方法
    public void setText(String s){
        et.setText(s);
    }
    public Editable getText(){
        return et.getText();
    }
    public void setEndable(boolean flag){
        et.setEnabled(flag);
    }


}

R.layout.myedittext文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="15dip"
          android:background="@color/white" >
          <EditText
              android:id="@+id/et"
              style="@style/normal_text_dark"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginBottom="15dip"
              android:layout_marginLeft="@dimen/view_space"
              android:layout_marginRight="5dp"
              android:layout_marginTop="15dip"
              android:gravity="top"
              android:paddingLeft="15dp"
              android:paddingTop="15dp"
              android:paddingRight="15dp"
              android:paddingBottom="45dp"
              android:background="@drawable/choose_button_bg"
              android:longClickable="false"
               />
          <TextView 
              android:id="@+id/tv"
              android:layout_marginBottom="15dp"
              android:layout_alignBottom="@id/et"
              android:layout_marginRight="25dp"
              android:layout_alignParentRight="true"
              android:layout_width="wrap_content"
              style="@style/normal_text_dark"
              android:layout_height="wrap_content"
              android:text="0/200"
              />
      </RelativeLayout>

@drawable/choose_button_bg文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <padding
        android:bottom="10dip"
        android:left="10dip"
        android:right="10dip"
        android:top="10dip" />

    <corners
        android:bottomLeftRadius="4dp"
        android:bottomRightRadius="4dp"
        android:topLeftRadius="4dp"
        android:topRightRadius="4dp" />

    <solid android:color="#ffffff" />

    <stroke
        android:width="0.1dip"
        android:color="@color/activity_text_dark" />

</shape>

下边是属性的命名,在attrs.xml 文件中。

  <!-- 自定义控件myEditView的最大长度 -->
   <declare-styleable name="MyEditText">
       <attr name="maxlength" format="integer" />
       <attr name="minlines" format="integer" />
   </declare-styleable>

重要的文件,都基本列完了,剩下的不太重要的,读者可以根据需要自己设置

最后是使用的方法,在布局文件中 的使用

<linnerlayout
 ...
 xmlns:myedittext="http://schemas.android.com/apk/res-auto">
 ...
 <com.babyrun.mmsh.widget.MyEditText
    android:id="@+id/service_location_myet"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    myedittext:minlines="3"
    myedittext:maxlength="50" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值