使用edittext,显示字数并提示剩余字数

来源:互联网 发布:文件加密软件绿色版 编辑:程序博客网 时间:2024/05/16 10:23

在项目中用到Edittext,需要显示提示,特此记录。

xml代码

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_change_info"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/background_dark"    android:orientation="vertical"    tools:context="com.thirdnet.cctv.activitys.ChangeInfoActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="48dp"        android:background="@color/white"        android:gravity="center_vertical">        <EditText            android:id="@+id/et"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:background="@null"            android:hint="请填写"            android:paddingLeft="10dp"            android:textSize="16sp" />    </LinearLayout>    <TextView        android:id="@+id/tv_size"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginRight="10dp"        android:gravity="right"        android:textSize="14sp"/></LinearLayout>

程序代码

pulbic class FillinActivity {    @BindView(et)    EditText etContent;    @BindView(R.id.tv_size)  @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //显示输入字符数量        showCharNumber(8);        //判断etContent.gettext()是否为空        if (TextUtils.isEmpty(etContent.getText().toString())) {                    tvSize.setText("0" + "/8");                } else                    tvSize.setText(etContent.getText().length() + "/8");        }    private void showCharNumber(final int maxNumber) {        etContent.addTextChangedListener(new TextWatcher() {            private CharSequence temp;            private int selectionStart;            private int selectionEnd;            @Override            public void beforeTextChanged(CharSequence s, int start, int count, int after) {                temp = s;            }            @Override            public void onTextChanged(CharSequence s, int start, int before, int count) {            }            @Override            public void afterTextChanged(Editable s) {                int number =  s.length();                tvSize.setText(number + "/" + maxNumber);                selectionStart = etContent.getSelectionStart();                selectionEnd = etContent.getSelectionEnd();                //System.out.println("start="+selectionStart+",end="+selectionEnd);                if (temp.length() > maxNumber) {                    s.delete(selectionStart - 1, selectionEnd);                    int tempSelection = selectionStart;                    etContent.setText(s);                    etContent.setSelection(tempSelection);                }            }        });    }}

效果还行,可以使用

0 0
原创粉丝点击