How to use the TextWatcher class in Android?

前端 未结 9 1023
独厮守ぢ
独厮守ぢ 2020-11-22 02:30

Can anyone tell me how to mask the substring in EditText or how to change EditText substring input to password type

相关标签:
9条回答
  • 2020-11-22 03:09
        public class Test extends AppCompatActivity {
    
        EditText firstEditText;
        EditText secondEditText;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.test);
            firstEditText = (EditText)findViewById(R.id.firstEditText);
            secondEditText = (EditText)findViewById(R.id.secondEditText);
    
            firstEditText.addTextChangedListener(new EditTextListener());
    
        }
    
        private class EditTextListener implements 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) {
                secondEditText.setText(firstEditText.getText());
            }
    
            @Override
            public void afterTextChanged(Editable s) {
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:12

    if you implement with dialog edittext. use like this:. its same with use to other edittext.

    dialog.getInputEditText().addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int start, int before, int count) {
        }
    
        @Override
        public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
            if (start<2){
                    dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false);
                }else{
                    double size =  Double.parseDouble(charSequence.toString());
                    if (size > 0.000001 && size < 0.999999){
                        dialog.getActionButton(DialogAction.POSITIVE).setEnabled(true);
                    }else{
                        ToastHelper.show(HistoryActivity.this, "Size must between 0.1 - 0.9");
                        dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false);
                    }
    
                }
        }
    
        @Override
        public void afterTextChanged(Editable editable) {
    
        }
    });
    
    0 讨论(0)
  • 2020-11-22 03:14

    Supplemental answer

    Here is a visual supplement to the other answers. My fuller answer with the code and explanations is here.

    • Red: text about to be deleted (replaced)
    • Green: text that was just added (replacing the old red text)

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