使用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
- 使用edittext,显示字数并提示剩余字数
- android EditText限制输入字数并显示剩余字数
- TextArea剩余字数显示并提示
- Edittext字数限制且显示剩余可输入字数
- iOS UITextView限制输入字数并显示剩余字数
- 剩余字数动态提示
- 对EditText输入字数限制并提示
- EditText设置字数限制并给出提示
- Android EditText最大字数限制并提示
- android EditText 字数监听并显示
- android EditText 字数监听并显示
- 监听editText字数计数并显示
- Android 自定义评价Dialog(RatingBar的使用和EditText的剩余字数显示)
- teaxtarea文本框剩余字数显示
- 自动显示文本输入剩余字数,并限制文本输入字数
- THINKPHP字数限制,简介等使用,显示规定字数,剩余用...显示
- js 限制输入的字数及其剩余字数的提示
- iOS UITextView 字数限制+提示剩余可输入字数
- 40个Java多线程问题总结
- php递归遍历目录下的文件
- Fixing Unsupported major.minor version 52.0 Error in Java
- tilcon下常用API函数说明
- 一天一条Linux指令-telnet
- 使用edittext,显示字数并提示剩余字数
- jqGrid学习记录
- PHP实现排序算法----冒泡排序(Bubble Sort)
- 【编程题】1000个(0-9)数字的数组中查找出现次数最多的3个数字,输出这3个数字和出现的次数。
- poj 2400 Supervisor, Supervisee
- 光照不变量的提取
- ue4 SpawnActor和SpawnAIFromClass
- Springboot下使用WebSocket和浏览器进行长连接通信
- 相对定位和绝对定位总结