Android(安卓)EditText部分特殊功能
16lz
2021-01-26
1、给EditText加上文字选中功能,比如微博的插入话题功能。点击“插入话题”按钮的时候,“#请插入话题名称#”在两个#号中间的内容处于选中状态,用户一点击即消失。代码如下:
- Java代码
- text.setText("#请插入话题名称#");
- Editableeditable=text.getText();
- Selection.setSelection(editable,1,editable.length()-1);
2、如果想默认进入一个Activity时,唯一的一个edittext先不要获得焦点。在EditText前面加上一个没有大小的Layout:
- XML/HTML代码
- <LinearLayout
- android:focusable="true"android:focusableInTouchMode="true"
- android:layout_width="0px"android:layout_height="0px"/>
3、输入文字的时候,如果想限制字数,并提示用户,可用以下方法:
- Java代码
- text.addTextChangedListener(newTextWatcher(){
- @Override
- publicvoidonTextChanged(CharSequences,intstart,intbefore,intcount){
- textCount=textCount+count-before;
- if(textCount<=140){
- writeWordDes.setText("可输入字数:"+(140-textCount));
- writeWordDes.setTextColor(getResources().getColor(R.color.solid_black));
- }else{
- writeWordDes.setText("超出字数:"+(textCount-140));
- writeWordDes.setTextColor(getResources().getColor(R.color.solid_red));
- }
- }
- @Override
- publicvoidbeforeTextChanged(CharSequences,intstart,intcount,
- intafter){
- }
- @Override
- publicvoidafterTextChanged(Editables){
- }
- });
- }
4、让EditText不可输入,比如超过一定字数后,不让用户再输入文字:
- Java代码
- text.setFilters(newInputFilter[]{
- newInputFilter(){
- publicCharSequencefilter(CharSequencesource,intstart,
- intend,Spanneddest,intdstart,intdend){
- returnsource.length()<1?dest.subSequence(dstart,dend):"";
- }
- }
- });
更多相关文章
- Android(安卓)自定义弹窗框架
- Flutter 项目代码打包进 android App的快速集成方案
- Android培训班(84)Dalvik虚拟机的JNI测试函数
- Eclipse+CDT+GDB 调试android NDK程序
- android 4.2 rtlSupport对UI性能的坑
- Android中Button设置background过程的研究
- gradle添加新的代码仓库
- Android和H5交互
- Ionic2实现自定义Cordova插件-Android屏幕录制