I have a few EditText's and one Button which will put string on EditText. The problem is, that I don't know how to do it to set text on focus EditText. When I'm on first EditText and press btn I want to set text on first EditText, and when focus another EditText and press btn I want text set where I focused. Please help
You can use a combination of a OnFocusChangeListener
and a variable to indicate which EditText
was last focused.
Lets say you have two EditText
. You would add a OnFocusChangeListener
to both. When the event fires, you just have to memorize which EditText received focus last. For example like this, using a class variable EditText lastFocused
:
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
lastFocused = (EditText)v;
}
Then, when a user presses your button you simply do:
public void onClick(View v) {
lastFocused.setText("Success!");
}