I have a class that creates a view to gather data via a function getView() that provides a view with an EditText.
This class has also has variable answer.
When the user chances the EditText I want to store the content of the EditText in answer.
If I would use an onKeyListener I fear that the answer will probably get stored before the last letter is entered.
Is there a good way to handle this in the getView() function via some other listener?
You should addTextChangedListener to your EditText and implement in your class TextWatcher
Then you will just take the text from the methods and store in your answer
Related
I'm struggling a bit with some semantics on some basic Android/Java development. If I have the following code (which works) to gather user input from a textfield:
final EditText userInput=(EditText) findViewById(R.id.txtUserInput);
Is userInput an object or a variable? My understanding that it is an object being instantiated form the *EditText * class. What does the (EditText) do to the left of the findViewById. When I see open parens, I think casting. Can anyone provide some simple clarity?
You are correct in saying that userinput is an EditText Object, to be more specific it is an object that is a subclass of View. Everything you get back from the findViewbyId() method will be a View, which you then need to cast to the proper Object. The (EditText) is casting the View you got back from your xml to an EditText. This allows you to access methods from the EditText that are available to the EditText class in particular.
So whenever you use findViewById() you also need to cast the View you get to the Object that it represents.
Let me know if you need further help.
-Dejan
userinput is an object.
findViewById(xxx) returns a View object, but in your case you know that it will return an EditText. Therefore its possible to cast it with (EditText). And you can cast it from a View to EditText since EditText extends View.
When you have cast it to EditText you are able to find all methods exposed by EditText instead of only the methods exposed in View.
I'm trying to user TextWatcher interface in order to detect which EditText was changed.
I have an activity with 10 EditTexts, and it looks weird to use 10 TextWatchers for each one of them.
There is any way to use only one TextWatcher and to use switch statement on the Editable in the functions afterTextChanged?
What I would do is to create a class that extends EditText and create a TextWatcher in that class. You can then implement those EditTexts in your XML or create them programmatically in Java with the TextWatcher listening for each EditText.
Don't know if this will work for you but you can give it a try.
I have never tried this before, but it should work if you check if the EditText is in focus. There are a few ways to go about this and the most straightforward one is to check the focus of the EditText inside your TextWatcher methods. You'll need to do something like this:
if(mEdit1.hasFocus()) {
...
} else if(mEdit2.hasFocus()) {
...
} else if(mEdit3.hasFocus()) {
...
}
A different approach would be to use an OnGlobalFocusChangeListener on your root view and set a variable indicating with EditText currently has focus. It would still require a lot of if statements to check for which EditText has the focus, but may be a more reusable solution.
I have a screen with an EditText and a ListView. As you type into the EditText, an AsyncTask is launched (any existing AsyncTask is cancelled and tossed) to query locally for some data to display in the ListView. If the EditText is empty of text, all data is queried for and displayed. Essentially, the EditText is a filter of the data on the screen. This is easy to do. In the addTextChangedListener, simply cancel your previous AsyncTask, launch a new one, and pass to it s.toString().
When the user hits the backspace to erase some text in the EditText, I want the default behavior (where all data populates the list). This works with my current implementation of addTextChangedListener.
However, I also have an X button on the right side of my EditText. This is intended to clear out the EditText (EditText.setText("");). This is a special case. In this case, I do not want the query to happen. The problem is, addTextChangedListener does not seem intelligent enough to know this. It gets triggered because the text has changed and all data is queried for.
How do I prevent this?
Use a boolean ignoreNextTextChangeKThxBye data member, setting/clearing that boolean as appropriate and using an if() statement to avoid doing the query.
I am using the the two editext and add the seperate textWatcher classes object
when some one give the focus then second one textListener is remove.
Then
My Question is how to check the textListener is add or remove
thnax in advance
AFAIK, There is no method to get the current assigne TextWatcher instance of the EditText object.
Rather you just can do like,
mEdiText.addTextChangedListener(null);
before you assign any TextWatcher and then assign the Watcher object mEdiText.addTextChangedListener(new MyTextWatcher()); to the EditText in any event or any condition.
Assigning null will remove the previous TextWatcher.
And to know is it really worked or not, just run your app and test it once.
Now I've got an EditText with TextWatcher which formats user input as a phone. For example if users input is 89011234567 it will be formatted as +7 (901) 123-45-67.
I wonder is there any way I can extend or implement some classes like CharSequence or Editableor any other like these to make EditText display the formatted output, but when I will call mEditText.getText().toString() it will return original users input?
You could create a class that extends the standard EditText, and then override the getText() method to return what you need.
public class MyEditText extends EditText {
//Implement the methods that need implementation. Calling the method in the super class should do the trick
#Override
public Editable getText() {
//return an Editable with the required text.
}
}
Then you just use the MyEditText instead of the normal EditText.
This solution will work, if you don't use the getText()-method other places (like your TextWatcher).
You could store the user's input into the tag property of your edittext, this way you could access it and it wouldn't be modified by using setText to display the formatted number !