限制字数, 并且实时显示字数的EditText

来源:互联网 发布:中金数据系统 编辑:程序博客网 时间:2024/05/03 20:30

最近换了新工作,有幸进入了一家C轮互联网公司,算是达到了职业规划的近期目标吧。

比起之前的小公司确实各方面都正规完善了很多,就Android来说,工程中对很多组件都进行了自己的封装。

前一阶段刚入公司,比较忙,现在已经缓和下来了,所以准备继续学习并写博客进行记录总结。

回归正题,今天下午写了一个简单的自定义控件,限制字数并且实时显示字数的EditText。

类文件

public class LimitScrollEditText extends LinearLayout {    private String hint;    private int maxLength;    private EditText content;    private TextView textCount;    private TextWatcher textWatcher;    public LimitScrollEditText(Context context) {        this(context, null);    }    public LimitScrollEditText(Context context, @Nullable AttributeSet attrs) {        this(context, attrs, 0);    }    public LimitScrollEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        initAttrs(attrs);        initView(context);        initData();    }    private void initAttrs(AttributeSet attrs) {        TypedArray arr = getContext().obtainStyledAttributes(attrs, R.styleable.LimitScrollEditText);        if (arr != null) {            hint = arr.getString(R.styleable.LimitScrollEditText_hint);            maxLength = arr.getInt(R.styleable.LimitScrollEditText_maxLength, 0);            arr.recycle();        }    }    private void initView(Context context) {        inflate(context, R.layout.layout_limit_scroll_edittext, this);        // 因为布局layout_limit_scroll_edittext使用了merge标签, 所以需要设置方向        setOrientation(VERTICAL);        content = (EditText) findViewById(R.id.content);        textCount = (TextView) findViewById(R.id.textCount);    }    private void initData() {        setHint(hint);        setMaxLength(maxLength);        setTextWatcher();    }    public void setHint(String hint) {        if (!TextUtils.isEmpty(hint)) content.setHint(hint);    }    public void setMaxLength(int maxLength) {        this.maxLength = Math.max(0, maxLength);        content.setFilters(new InputFilter[]{new InputFilter.LengthFilter(this.maxLength)});        setTextCount();    }    private void setTextWatcher() {        textWatcher = new TextWatcher() {            @Override            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {            }            @Override            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {            }            @Override            public void afterTextChanged(Editable editable) {                setTextCount();            }        };        content.addTextChangedListener(textWatcher);    }    private void setTextCount() {        if (TextUtils.isEmpty(content.getText())) {            textCount.setText("0/" + maxLength);        } else {            textCount.setText(content.getText().toString().length() + "/" + maxLength);        }    }}

xml文件

<?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ScrollView        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:padding="10dp">        <EditText            android:id="@+id/content"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:background="@null"            android:textSize="14sp"            tools:hint="请输入文字" />    </ScrollView>    <TextView        android:id="@+id/textCount"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentRight="true"        android:gravity="right|center_vertical"        android:paddingRight="10dp"        android:textSize="14sp"        tools:text="0/10" /></merge>

资源文件

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="LimitScrollEditText">        <attr name="hint" format="string" />        <attr name="maxLength" format="integer" />    </declare-styleable></resources>


使用起来,很简单,如下:

Activity文件:

public class LimitScrollEditTextActivity extends Activity {    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_limit_scroll_edittext);        initView();    }    private void initView() {        LimitScrollEditText limitScrollEditText = (LimitScrollEditText) findViewById(R.id.limitScrollEditText);        limitScrollEditText.setMaxLength(200);        limitScrollEditText.setHint("最多输入200字");    }}

还有xml文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <com.example.tsnt.view.LimitScrollEditText.LimitScrollEditText        android:id="@+id/limitScrollEditText"        android:layout_width="match_parent"        android:layout_height="100dp"        app:hint="Hello world!"        app:maxLength="10" /></LinearLayout>

效果图

这里写图片描述

源代码地址:https://github.com/tingshuonitiao/AndroidStudy.git

原创粉丝点击