只是控制字数不显示输入字数只需加这行代码就行
editContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(max)});
1:需要加入监听输入字数下面代码实现
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="@+id/edit_content"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#80000000"
android:gravity="top"
android:maxLength="300"
android:minLines="5"
android:padding="12dp"
android:textColor="#ffffff"
android:textSize="14sp" />
<TextView
android:id="@+id/publish_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/edit_content"
android:layout_alignParentEnd="true"
android:textColor="#ffffff"
android:text="0/300" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
2代码块
package com.demo.cn;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import butterknife.BindView;
import butterknife.ButterKnife;
public class Main2Activity extends AppCompatActivity {
@BindView(R.id.edit_content)
EditText editContent;
@BindView(R.id.publish_num)
TextView publishNum;
private int max = 20;//最大输入字符
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ButterKnife.bind(this);
initData();
}
private void initData() {
//输入框字数限制
InputFilter[] filters = {chineseFilter()};
editContent.addTextChangedListener(passwordListener());
editContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(max)});
}
/**
* 输入框
*
* @return
*/
private InputFilter chineseFilter() {
return new InputFilter() {
String regEx = "[\\u4e00-\\u9fa5]"; // unicode编码,判断是否为汉字
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
float destCount = dest.toString().length()
+ getChineseCount(dest.toString());
float sourceCount = source.toString().length()
+ getChineseCount(source.toString());
if (destCount + sourceCount > 10) {
Log.e("log", "已经达到字数限制范围");
return "";
} else {
return source;
}
}
private float getChineseCount(String str) {
float count = 0;
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
while (m.find()) {
for (int i = 0; i <= m.groupCount(); i++) {
count = count + 1;//
}
}
return count;
}
};
}
private TextWatcher passwordListener() {
return new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int length = s.length();
if (length > max) {
Toast.makeText(Main2Activity.this, "最多可输入" + max + "个字", Toast.LENGTH_SHORT).show();
} else {
publishNum.setText(length + "/" + max);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
}
}