每次更改后Android系统的变化EDITTEXT [英] Android-Change Edittext after each change

查看:265
本文介绍了每次更改后Android系统的变化EDITTEXT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何添加字符,如破折号这个' - ',例如在edtitext每次更改后,如果用户输入的一个那么的EditText文本将是A-那么用户将完成,并进入字符B,则该EditText上会AB
如何实现这一点?谢谢

  NAME =(EditText上)findViewById(R.id.editText1);
        name.addTextChangedListener(新TextWatcher(){
             公共无效afterTextChanged(编辑S){                 name.setText( - );
                }
     公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数后INT){}
       公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
               }


解决方案

您是具有如Android的文档中描述的无限循环


  

但要小心不要让自己陷入死循环,因为你所做的任何更改将导致再次递归调用此方法。


因此​​,所有你需要做的仅仅是强加的条件,以避免无限循环。例如,

  name.addTextChangedListener(新TextWatcher(){
        公共无效afterTextChanged(编辑S){
            如果(s.charAt(s.length() - 1)!=' - '){
                s.append( - );
            }        }        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
                INT后){
        }        公共无效之前onTextChanged(CharSequence中,诠释开始,诠释,
                诠释计数){
        }    });

how can i add Char such as this dash '-' after each change in edtitext for example if the user enter A then the text in edittext will be A- then the user will complete and enter Char B then the edittext will be A-B How to implement this ? thanks

name = (EditText)findViewById(R.id.editText1);
        name.addTextChangedListener(new TextWatcher(){
             public void afterTextChanged(Editable s) {

                 name.setText("-");
                }
     public void beforeTextChanged(CharSequence s, int start, int count, int after){}
       public void onTextChanged(CharSequence s, int start, int before, int count){


               }

解决方案

You are having infinite loop as described in Android Doc

but be careful not to get yourself into an infinite loop, because any changes you make will cause this method to be called again recursively.

So all you have to do is just imposing a condition to avoid infinite loop. For example,

name.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            if(s.charAt(s.length()-1)!='-'){
                s.append("-");
            }

        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
        }

    });

这篇关于每次更改后Android系统的变化EDITTEXT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆