Johan
Johan

Reputation: 1

Multiple EditTexts

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

Answers (4)

divim
divim

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

Alepac
Alepac

Reputation: 1831

Check if the View v parameter equals the instance of your EditText.

if(hasFocus && v == et1) {
   ...
}

Upvotes: 0

class stacker
class stacker

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

d'alar'cop
d'alar'cop

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

Related Questions