Java – Android TextWather object. How do Java anonymous classes work?

Android TextWather object. How do Java anonymous classes work?… here is a solution to the problem.

Android TextWather object. How do Java anonymous classes work?

private TextWatcher billEditTextWatcher = new TextWatcher() 
       called when the user enters a number
      public void onTextChanged(CharSequence s, int start, 
         int before, int count) 
          convert billEditText's text to a double
            currentBillTotal = Double.parseDouble(s.toString());
         } // end try
         catch (NumberFormatException e)
            currentBillTotal = 0.0;  default if an exception occurs
         } // end catch 

 update the standard and custom tip EditTexts
         updateStandard();  update the 10, 15 and 20% EditTexts
         updateCustom();  update the custom tip EditTexts
      } // end method onTextChanged

      public void afterTextChanged(Editable s) 
      } // end method afterTextChanged

      public void beforeTextChanged(CharSequence s, int start, int count,
         int after) 
      } // end method beforeTextChanged
   };  end billEditTextWatcher

This is a piece of code for a tip calculator app written by professionals. Can someone explain how this works?

Usually I just write the following to create a new object.

TextWatcher billEditTextWatcher = new TextWatcher();

I understand private work. But how can there be a way to create new objects? Does it basically do what it says? Overriding the original method in the TextWatcher class?


hope this question makes sense because I’m confused.

Thanks in advance!


Here is an example of an anonymous class in Java. You don’t have any TextWatcher java files, and you declare the contents of the class when you initialize it.

Related Problems and Solutions