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

最近换了新工作,有幸进入了一家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

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值