Android user preference type loses focus when typing

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

Any idea why this is losing focus?

Here is what I did to create the view. bloated layout is just a basic linear layout with title text in it.

I could change it to a preference for dialogue, but I think, but it would be smoother if it could be part of the basic representation.

@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;
}

List item:

<?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>

may have been a standard list item and looked identical.

+3
source share
2

, :

:

  • getContext()/ ?
  • R.layout.auto_complete_text_list_item?
  • getEntries()? String [] .
  • setOnItemSelectedListener (this) - , , OnItemSelectedListener - requestFocus() - ... , . , (..

, xml . , . , , , AutoCompleteTextView, .

, ! , , .

0

, ListView.

, OnFocusChangeListener(), , , . , Android, .

, Asus 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();
    }
}

. , .

0

Source: https://habr.com/ru/post/1781424/


All Articles