This is an example of an anonymous class in Java. You don't have any TextWatcher
java file, and you declare the content of the class while initializing it.
http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
Question
private TextWatcher billEditTextWatcher = new TextWatcher()
{
// called when the user enters a number
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count)
{
// convert billEditText's text to a double
try
{
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
@Override
public void afterTextChanged(Editable s)
{
} // end method afterTextChanged
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after)
{
} // end method beforeTextChanged
}; // end billEditTextWatcher
This is a segment of a code from a tip calculator app written by a professional. Can someone explain how this works?
Usually I just write the following to create a new object.
TextWatcher billEditTextWatcher = new TextWatcher();
I understand what the private does. But how come there are methods in the creation of the new object? Is it basically doing what it says? overriding the original methods in the TextWatcher class?
I hope this question makes sense because I'm quiet confused.
Thanks in advance!
Solution
This is an example of an anonymous class in Java. You don't have any TextWatcher
java file, and you declare the content of the class while initializing it.
http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
OTHER TIPS
As name is indicating TextWatcher will sense all event of EditText
Like when you are writing something in Editable area.
Its have following callback(Watching state.)
beforeTextChanged();
afterTextChanged();
onTextChanged();
All the callback method contain their relative data which passed by event generator. like which is key is pressed , Unicode(ASCII) of Key etc.
Basically, a TextWatcher is used to keep watch on EditText or MultiLine EditText while entering data into it. We can perform operation and keep watch on which characters are being entered or how many characters are being entered in the EditText.
Technical Description:
TextWatcher
then you need to resister your EditText
with TextWather
object.e.g.
EditText editTextPassword; // Some EditText object.
TextWatcher billEditTextWatcher = new TextWatcher(); // TextWather object creation
editTextPassword.addTextChangedListener(billEditTextWatcher ); // EditText registation with Textwather object.
By default all call back of TextWather are empty that's that why you need yo give your definition of all callback according to your requirement.
private TextWatcher billEditTextWatcher = new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// you code is here while onTextChanged.
}
@Override
public void afterTextChanged(Editable s)
{
you code is here while afterTextChanged.
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after)
{
// you code is here while beforeTextChanged.
}
};