Call the Textwatcher method

Asked

Viewed 174 times

0

Good afternoon! I’m starting my studies in Java and I’ve come to this topic. What I call the method insert on the side of Activity, past the field EditText defendant?

public static TextWatcher insert(final EditText editText){
    return new TextWatcher() {
    ...
    public void onTextChanged(CharSequence cs, int start, int before, int count){
        editText.setText("Qualquer coisa");
  • Post the complete code because this logic is very confusing. How are you setting value to Edittext during the onTextChanged event? See the loop: the changed text calls the event, the event changes the text... This will not work.

  • So Reginaldo, I took the code you posted here with that Dynamic Mask for Cpf;cnpf and I’m slowly unlocking it to understand it: On the Activity side you have the fields below: report=(Textview)findViewById(R.id.txtReport); password=(Edittext)findViewById(R.id.edtPassword); I just want to change the Edittext(password) field for example, the Label(report) field receives a Text or the Edittext field itself changes, just to know that it’s working, I don’t know how to call it from the Activity side.

  • After initializing the object like this: report=(Textview)findViewById(R.id.txtReport); you can assign text to it like this: report.setText("Test");

  • Yes yes, I’m trying to call an external class , inside a package that returns a Textwatcher , how to call it inside my Activity?

2 answers

0

How your method this static you will need to refer to Activity

Example

EditText myEditText = (EditText) findViewById(R.id.edit_text);
MyActivity.insert(myEditText);

I hope it helps you!

0

In the Activity where reporting is initialized you can add the textWatcher to Edit to receive message when editText is changed, thus:

        reportar=(TextView)findViewById(R.id.txtReport);        
        reportar.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) {
        }

        @Override
        public void afterTextChanged(Editable s) {
         /* insira código que o ocorre quando edit reportar for alterado.
menos mudar o próprio edit reportar. */



        }
    });
  • Actually I want to take the Textwatcher return from an external class and use in some Act public Static Textwatcher Insert(final Edittext field, final Textview text){ Return new Textwatcher() { @Override public void onTextChanged(Charsequence s, int start, int Count, int after) { String mascara= "Set label text"; text.setText(mascara); } Inside Activity : Class.Insert(Edittext,Textview), here I am passing the parameters, when and how I get this return and the label field changes.

  • What you’ve commented on works fine, but for example: if I want to use an instantiated class within my Activity , where the Textview value gets a value as soon as Edittext is changed, as I would call this Addtextchangedlistener , since the value is coming from the other side? I don’t know if I expressed myself well... :(

  • Reginaldo, just one more question: in this snippet when it changes the value of the Edittext field to receive the mask-formatted field because it gives no error: isUpdating = true; editText.setText(mascara); editText.setSelection(mascara.length());

  • What error are you getting?

  • So: if I Seto any value in Edittext at runtime hangs, but in this case it will insert the score and does not lock.

  • Create another project, put only one edittext and use this code. You will see it working and should conclude that the problem is not there. Must be somewhere else in the code.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.