android学习-editText实时监控

版权声明:转载标明来源 https://blog.csdn.net/qq_33347077/article/details/81908698

android学习-EditText实时监控

android中需要实时监控EditText的输入框,并实时读取框中内容进行显示

editText.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) {
                @编辑框改变的时候进行调用
                textView.setText(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
                @编辑框改变之后进行调用
            }
        });

本人在这里需要对EditText中的内容转化为int类型,需要注意EditText中字符的个数,如果字符数为零直接设该数值为零。

  @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                @编辑框改变的时候进行调用
                int a;
                if(s.length()!=0){
                    a = Integer.parse(s.toString());
                }else 
                {
                    a = 0;
                }
                textView.setText(a+"");
            }

问题

  • 光标位置的操作不够灵活

光标位置的操作需要通过editText.setSelection()函数来进行实现,一般情况下采用默认不进行指定即可

猜你喜欢

转载自blog.csdn.net/qq_33347077/article/details/81908698