How to use EditText onTextChanged event when I press the number?

前端 未结 6 527
自闭症患者
自闭症患者 2020-11-30 06:40

I have an EditText with \"text = 0.00\". When I press the number 3, it should be like 0.03 and the second time when I press the number

相关标签:
6条回答
  • 2020-11-30 06:57

    In Kotlin Android EditText listener is set using,

       val searchTo : EditText = findViewById(R.id.searchTo)
       searchTo.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable) {
    
            // you can call or do what you want with your EditText here
    
            // yourEditText...
        }
    
        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
    })
    
    0 讨论(0)
  • 2020-11-30 06:58

    You can also try this:

    EditText searchTo = (EditText)findViewById(R.id.medittext);
    searchTo.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            doSomething();
        } 
    });
    
    0 讨论(0)
  • 2020-11-30 06:58

    put the logic in

    afterTextChanged(Editable s) {
        string str = s.toString()
        // use the string str
    }
    

    documentation on TextWatcher

    0 讨论(0)
  • 2020-11-30 07:03

    Here, I wrote something similar to what u need:

        inputBoxNumberEt.setText(".     ");
        inputBoxNumberEt.setSelection(inputBoxNumberEt.getText().length());
        inputBoxNumberEt.addTextChangedListener(new TextWatcher() {
    
            boolean ignoreChange = false;
    
            @Override
            public void afterTextChanged(Editable s) {
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
                if (!ignoreChange) {
                    String string = s.toString();
                    string = string.replace(".", "");
                    string = string.replace(" ", "");
                    if (string.length() == 0)
                        string = ".     ";
                    else if (string.length() == 1)
                        string = ".  " + string;
                    else if (string.length() == 2)
                        string = "." + string;
                    else if (string.length() > 2)
                        string = string.substring(0, string.length() - 2) + "." + string.substring(string.length() - 2, string.length());
                    ignoreChange = true;
                    inputBoxNumberEt.setText(string);
                    inputBoxNumberEt.setSelection(inputBoxNumberEt.getText().length());
                    ignoreChange = false;
                }
            }
        });
    
    0 讨论(0)
  • 2020-11-30 07:07

    To change the text;

    multipleLine.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) {
                MainActivity.myArray.set(pickId,String.valueOf(s));
                MainActivity.myAdapt.notifyDataSetChanged();
            }
    
            @Override
            public void afterTextChanged(Editable s) {
    
            }
    
    0 讨论(0)
  • 2020-11-30 07:11

    You have selected correct approach. You have to extend the class with TextWatcher and override afterTextChanged(),beforeTextChanged(), onTextChanged().

    You have to write your desired logic in afterTextChanged() method to achieve functionality needed by you.

    0 讨论(0)
提交回复
热议问题