Reputation: 1
I have multiple edittexts in multiple tablerows. I know I can access them thru findViewById. Is there a way to find out in onFocusChange which edittext has focus, instead of using findViewById?
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
int edit1 = 0, edit2 = 0, result = 0;
if(hasFocus == true)
{
}
else {
edit1 = Integer.parseInt(et1.getText().toString());
result = Integer.parseInt(et3.getText().toString());
edit2 = result - edit1;
et2.setText(Integer.toString(edit2));
et3.setText(Integer.toString(edit2));
}
}
Upvotes: 0
Views: 229
Reputation: 61
You can setTag() in each edittext view and set focus listener in each view.If you don't want to find this using findViewById() than you can retrieve view from parent view programmaticly and assign them tag and set click listener.
Upvotes: 0
Reputation: 1831
Check if the View v parameter equals the instance of your EditText.
if(hasFocus && v == et1) {
...
}
Upvotes: 0
Reputation: 5347
When the user switches from one EditText to another, onFocusChange()
will be called twice; once for the View which lost focus, and a second time for the View which now has it. (Edit: This sounds like there was an order in which these events arrive, but even if that were the case, it's not guaranteed and you must not rely on it.)
So in your hasFocus == true
branch, you simply inspect View v
, see whether it's an instanceof
EditText
and there you have your View object.
Upvotes: 1
Reputation: 2365
getWindow().getCurrentFocus();
Of course, you will need to do something clever after this - even just a try/catch assuming that the focus is an EditText might be enough.
Upvotes: 1