开发者

Custom android preference type loses focus when typing

I created a simple preference class that shows an AutoCompleteTextView control and it displays properly but when i focus on the AutoCompleteTextView and start typing it brings up the keyboard but then immediately loses focus on the control.

Any idea why this loses focus?

Here's what i did to create the view. the inflated layout is just a basic linear layout with a title textview in it.

I could change it to a dialo开发者_如何学编程g preference instead I guess but it'd be smoother if it could be part of the base view.

@Override
protected View onCreateView(ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.base_preference, null);

    if (mHint != null) {
        TextView hintView = (TextView) layout.findViewById(R.id.PreferenceHintTextView);
        hintView.setText(mHint);
    }

    TextView titleView = (TextView) layout.findViewById(R.id.PreferenceTitleTextView);
    titleView.setText(getTitle());

    AutoCompleteTextView inputView = new AutoCompleteTextView(getContext());
    inputView.setGravity(Gravity.FILL_HORIZONTAL);

    ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getContext(),
            R.layout.auto_complete_text_list_item,
            getEntries());

    inputView.setAdapter(adapter);
    inputView.setThreshold(1);
    inputView.setOnItemSelectedListener(this);
    layout.addView(inputView);

    return layout;
}

The list item is:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp"
    android:textColor="#000">
</TextView>

could've probably been the standard list item and come out identical.


I spent a bit of time trying to re-create this issue for you:

Couple of questions:

  1. What is getContext() / getting the context from?
  2. Can you show your R.layout.auto_complete_text_list_item? The standard list item worked for me
  3. Can you show the getEntries() method? After testing with a small String[] array, it worked for me.
  4. setOnItemSelectedListener(this) - can you show the method you're using for this as it implements OnItemSelectedListener - you might need requestFocus() here or are doing something odd to pull focus away from the control... although this is for selected and not click. I'm not sure if this is firing as an auto-select by the list itself (i.e. defaulting to the first item

Lastly, try setting the component into an xml and not doing this dynamically to test. That was the first factor I tried to control. I know you'll want this to be dynamic but it's not necessarily a bad thing to create a custom AutoCompleteTextView control that uses its own layout.

Hope these help! A project would certainly help me review and assist.


I had the same issues and assumed preferences are handled within a ListView.

So in the OnFocusChangeListener() call when focus is gained, I call this method to fix the issue. Note the issue does not occur on every device or Android versions, which can make it very hard to detect in the first place.

Issue reproduced on an Asus device running Android 4.1.1.

private boolean fixedAlready = false;
private void fixListViewFocusability(View v)
{
    if (fixedAlready)
        return;

    Log.d(at_data.TAG, "Trying to fix focus issues from " + v);

    ViewGroup vg = (ViewGroup) v.getParent();
    while (vg != null)
    {
        if (vg instanceof ListView)
        {
            Log.d(at_data.TAG, "Found list view " + vg);
            ListView lv = (ListView) vg;

            fixedAlready = true;
            lv.setDescendantFocusability(ListView.FOCUS_AFTER_DESCENDANTS);

            break;
        }

        vg = (ViewGroup) vg.getParent();
    }
}

The above worked for me on that particular device. Not sure how other devices will react though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜