I have an EditText that I’m using as the footer View for my ListView. I’m attempting to access the focus / lose focus events, but for some reason, it’s firing multiple times per focus. What is going on here?
// in onCreate
// ...
// the footer
View v = getLayoutInflater().inflate( R.layout.comment_edittext, null );
EditText commentEditText = (EditText)v.findViewById( R.id.comment_edittext );
commentEditText.setOnFocusChangeListener( new OnFocusChangeListener() {
@Override
public void onFocusChange( View v, boolean hasFocus )
{
U.log("View: " + v.getClass().getName().toString() );
if( hasFocus )
{
U.log( "Clicked" );
}
else
{
U.log( "Un Clicked" );
}
}
} );
// add the footer
commentListView.addFooterView( v );
// ...
The output, of a single touch, which brings up the keyboard is:
04-11 10:22:17.449: E/004 - X(4576): View: android.widget.EditText
04-11 10:22:17.459: E/004 - X(4576): Clicked
04-11 10:22:17.569: E/004 - X(4576): View: android.widget.EditText
04-11 10:22:17.569: E/004 - X(4576): Un Clicked
04-11 10:22:17.569: E/004 - X(4576): View: android.widget.EditText
04-11 10:22:17.569: E/004 - X(4576): Clicked
04-11 10:22:17.689: E/004 - X(4576): View: android.widget.EditText
04-11 10:22:17.689: E/004 - X(4576): Un Clicked
04-11 10:22:17.709: E/004 - X(4576): View: android.widget.EditText
04-11 10:22:17.709: E/004 - X(4576): Clicked
I can kind of understand it calling multiple events with hasFocus set to true, but why then are there also falses, triggering the Un Clicked log?
EDIT: Furthermore, whenever I scroll the ListView up and down, moving the EditText in and out of view, it’s calling these events as well. Am I using the wrong listener type perhaps?
From personal experience, EditTexts inside ListViews are a bane. Because of the way views are recycled and rendered, the focus behaves strangely. I recommend approaching the problem some other way. Actually, if it’s a comment text box, wouldn’t it be better if it was always visible?
Sample layout: