android edittext onchange listener

前端 未结 9 2188
长情又很酷
长情又很酷 2020-11-27 13:28

I know a little bit about TextWatcher but that fires on every character you enter. I want a listener that fires whenever the user finishes editing. Is it possib

相关标签:
9条回答
  • 2020-11-27 13:35

    As far as I can think bout it, there's only two ways you can do it. How can you know the user has finished writing a word? Either on focus lost, or clicking on an "ok" button. There's no way on my mind you can know the user pressed the last character...

    So call onFocusChange(View v, boolean hasFocus) or add a button and a click listener to it.

    0 讨论(0)
  • 2020-11-27 13:35

    The Watcher method fires on every character input. So, I built this code based on onFocusChange method:

    public static boolean comS(String s1,String s2){
        if (s1.length()==s2.length()){
            int l=s1.length();
            for (int i=0;i<l;i++){
                if (s1.charAt(i)!=s2.charAt(i))return false;
            }
            return true;
        }
        return false;
    }
    
    public void onChange(final EditText EdTe, final Runnable FRun){
        class finalS{String s="";}
        final finalS dat=new finalS();  
        EdTe.setOnFocusChangeListener(new OnFocusChangeListener() {          
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {dat.s=""+EdTe.getText();}
                else if (!comS(dat.s,""+EdTe.getText())){(new Handler()).post(FRun);}       
            }
        });
    }
    

    To using it, just call like this:

    onChange(YourEditText, new Runnable(){public void run(){
        // V  V    YOUR WORK HERE
    
        }}
    );
    

    You can ignore the comS function by replace the !comS(dat.s,""+EdTe.getText()) with !equal function. However the equal function itself some time work not correctly in run time.

    The onChange listener will remember old data of EditText when user focus typing, and then compare the new data when user lose focus or jump to other input. If comparing old String not same new String, it fires the work.

    If you only have 1 EditText, then u will need to make a ClearFocus function by making an Ultimate Secret Transparent Micro EditText

    0 讨论(0)
  • 2020-11-27 13:38

    First, you can see if the user finished editing the text if the EditText loses focus or if the user presses the done button (this depends on your implementation and on what fits the best for you). Second, you can't get an EditText instance within the TextWatcher only if you have declared the EditText as an instance object. Even though you shouldn't edit the EditText within the TextWatcher because it is not safe.

    EDIT:

    To be able to get the EditText instance into your TextWatcher implementation, you should try something like this:

    public class YourClass extends Activity {
    
        private EditText yourEditText;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.main);
            yourEditText = (EditText) findViewById(R.id.yourEditTextId);
    
            yourEditText.addTextChangedListener(new TextWatcher() {
    
                public void afterTextChanged(Editable s) {
    
                    // you can call or do what you want with your EditText here
    
                    // yourEditText... 
                }
    
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    
                public void onTextChanged(CharSequence s, int start, int before, int count) {}
            });
        }
    }
    

    Note that the above sample might have some errors but I just wanted to show you an example.

    0 讨论(0)
  • 2020-11-27 13:38

    Anyone using ButterKnife. You can use like:

    @OnTextChanged(R.id.zip_code)
    void onZipCodeTextChanged(CharSequence zipCode, int start, int count, int after) {
    
    }
    
    0 讨论(0)
  • 2020-11-27 13:46

    TextWatcher didn't work for me as it kept firing for every EditText and messing up each others values.

    Here is my solution:

    public class ConsultantTSView extends Activity {
        .....
    
        //Submit is called when I push submit button.
        //I wanted to retrieve all EditText(tsHours) values in my HoursList
    
        public void submit(View view){
    
            ListView TSDateListView = (ListView) findViewById(R.id.hoursList);
            String value = ((EditText) TSDateListView.getChildAt(0).findViewById(R.id.tsHours)).getText().toString();
        }
    }
    

    Hence by using the getChildAt(xx) method you can retrieve any item in the ListView and get the individual item using findViewById. And it will then give the most recent value.

    0 讨论(0)
  • 2020-11-27 13:47

    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)
提交回复
热议问题