onTextChanged()在EditText中无法正常工作

时间:2018-05-24 07:15:23

标签: android android-edittext

  

我的目的是将我的edittext视图初始化为0.00。   输入一个数字ex-2后,编辑文本应显示为 - 0.02   然后我输入第二个ex-3编辑文本应该显示 - 0.23   在下一个数字ex -5之后,编辑文本应显示 - 2.35    对于这种情况,我已经实现了小代码。但是这没有按预期工作。 Edittext不会更新。

    public class MainActivity extends Activity {

    private EditText text;
    Character[] amount;
    int counter=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (EditText)findViewById(R.id.text);
        text.setSelection(text.getText().length());
        amount =toCharacterArray(text.getText().toString());

        text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                System.out.println("test "+s);
                if(s.length()>0)
                {

                    if(counter==0 && s.length()>=5)
                    {
                        Log.e("Edit counter 1",String.valueOf(s) );
                        amount[3]=s.charAt(4);
                    }
                    else if (counter==1&& s.length()>=5)
                    {
                        Log.e("Edit counter 2",String.valueOf(s) );
                        amount[2]=amount[3];
                        amount[3]=s.charAt(4);
                    }
                    else if (counter==2 && s.length()>=5)
                    {
                        amount[0]=amount[2];
                        amount[2]=amount[3];
                        amount[3]=s.charAt(4);
                    }
                    else
                    {

                    }


                    String str = "";

                    for (Character c : amount)
                        str += c.toString();

                    System.out.println("Edit test "+str);


                   text.setText(str);

                }
                counter++;

            }

            @Override
            public void afterTextChanged(Editable s) {
                System.out.println(s.toString());


            }

        });
    }
    public Character[] toCharacterArray( String s ) {

        if ( s == null ) {
            return null;
        }

        int len = s.length();
        Character[] array = new Character[len];
        for (int i = 0; i < len ; i++) {
            array[i] = new Character(s.charAt(i));
        }

        return array;
    }
}

2 个答案:

答案 0 :(得分:0)

您可以保留一个标志字符串,以指示即将推出的可编辑是来自UI输入还是您的代码。

       findViewById<EditText>(R.id.editText).addTextChangedListener(object : TextWatcher {

        var lastString: String = ""

        override fun afterTextChanged(s: Editable?) {
            if (s.toString() == lastString) {
                return
            }
            //customized
            lastString = "${s.toString()}1"
            s?.replace(0, s.toString().length, lastString)
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

        }

    })

答案 1 :(得分:0)

在为一致性更改而更改文本后再进行其他操作

private Boolean isUserInput =true;

text.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            System.out.println("test "+s);
            if(s.length()>0 && isUserInput)
            {

                if(counter==0 && s.length()>=5)
                {
                    Log.e("Edit counter 1",String.valueOf(s) );
                    amount[3]=s.charAt(4);
                }
                else if (counter==1&& s.length()>=5)
                {
                    Log.e("Edit counter 2",String.valueOf(s) );
                    amount[2]=amount[3];
                    amount[3]=s.charAt(4);
                }
                else if (counter==2 && s.length()>=5)
                {
                    amount[0]=amount[2];
                    amount[2]=amount[3];
                    amount[3]=s.charAt(4);
                }
                else
                {

                }


                String str = "";

                for (Character c : amount)
                    str += c.toString();

                System.out.println("Edit test "+str);


               text.setText(str);
               isUserInput = false;
            }
            counter++;

        }

 @Override
 public void afterTextChanged(Editable s) {       
         isUserInput = true;        
   }