Implementing Text Watcher for EditText

前端 未结 4 1160
北海茫月
北海茫月 2020-11-27 07:20

I have an EditText. When i click on it, it becomes focusable. I will type the input text to be entered into the EditText. I want to implement a listener for EditText, so tha

相关标签:
4条回答
  • 2020-11-27 07:22

    Also you can use debounce operator of Rx java.

    subject.debounce(300, TimeUnit.MILLISECONDS)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .map(yourString -> {
                               // save to db
                            }
                            return "";
                        })
                        .subscribe()
    

    Here you can define time limit , after how much time you want it to get saved in db.

    0 讨论(0)
  • 2020-11-27 07:25

    Add This to your editText

    et1.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                // 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 afterTextChanged(Editable s) {
    
                // TODO Auto-generated method stub
            }
        });
    
    0 讨论(0)
  • 2020-11-27 07:29

    set edittext imeOption

    editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
    

    By using something like this,

    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                // Specify your database function here.
                return true;
            }
            return false;
        }
    });
    

    Alternatively, you can use the OnEditorActionListener interface to avoid the anonymous inner class.

    0 讨论(0)
  • 2020-11-27 07:42

    Try like this.

    EditText et = (EditText)findViewById(R.id.editText);
    Log.e("TextWatcherTest", "Set text xyz");
    et.setText("xyz");
    
    et.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) { }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
        @Override
        public void afterTextChanged(Editable s) {
            Log.e("TextWatcherTest", "afterTextChanged:\t" +s.toString());
        }
    });
    
    0 讨论(0)
提交回复
热议问题