When the soft keyboard appears, it makes my EditText field lose focus

梦想与她 提交于 2019-11-28 03:03:58
SoloPilot

Here is how I did it. The onFocusChangeListener() is called several times when you touch a EditText to type text into it. The sequence is:

  1. If focus was on a different view, then that view loses focus
  2. The target gains focus
  3. Soft keyboard pops up.
  4. This causes the target to lose focus
  5. The code detects this situation and calls target.requestFocus()
  6. The leftmost, topmost view gains focus, due to Android nonsense
  7. The leftmost view loses focus, due to requestFocus being called
  8. Target finally gains focus

    //////////////////////////////////////////////////////////////////
    private final int minDelta = 300;           // threshold in ms
    private long focusTime = 0;                 // time of last touch
    private View focusTarget = null;
    
    View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            long t = System.currentTimeMillis();
            long delta = t - focusTime;
            if (hasFocus) {     // gained focus
                if (delta > minDelta) {
                    focusTime = t;
                    focusTarget = view;
                }
            }
            else {              // lost focus
                if (delta <= minDelta  &&  view == focusTarget) {
                    focusTarget.post(new Runnable() {   // reset focus to target
                        public void run() {
                            focusTarget.requestFocus();
                        }
                    });
                }
            }
        }
    };
    

The code above works well for the keyboard pop-ups. However, it does not detect the speech-to-text pop-up.

You need to change in your AndroidManifest.xml

Add android:windowSoftInputMode="adjustPan" in the activity holding the listview. This will solve your problem.

    <activity android:name=".MyEditTextInListView"
              android:label="@string/app_name"
              android:windowSoftInputMode="adjustPan">

Regards

In my case, this is happening because when the ListView resizes, it re-creates all of the list items (i.e. it calls getView() again for each visible list item).

Because the EditText is within the layout that I'm returning from getView(), this means that it's a different instance of EditText than the one which had the focus previously. A secondary consequence is that when the soft-keyboard appears or disappears I found that I was losing the contents of the EditText.

Because I wanted my view to remain fully accessible (i.e. I want it to be resized instead of hidden behind the keyboard window with some parts not accessible), I couldn't use Frank's answer, which otherwise seems like the best approach.

I solved this by using an OnFocusChangeListener on the EditText to record the timestamp when the focus was lost, and then in getView() when recreating the list item, if the current time is within some threshold from when the focus was lost, call requestFocus() to give it back to the EditText in question.

You can also grab the text from the previous instance of the EditText at that point and transfer it to the new instance.

private class MyAdapter<Type> extends ArrayAdapter<String>
    implements OnFocusChangeListener
{
    private EditText mText;
    private long mTextLostFocusTimestamp;
    private LayoutInflater mLayoutInflater;

    public MyAdapter(Context context, int resource, int textResourceId, ArrayList<String> data, LayoutInflater li) {
        super(context, resource, textResourceId, data);
        mLayoutInflater = li;
        mTextLostFocusTimestamp = -1;
    }

    private void reclaimFocus(View v, long timestamp) {
        if (timestamp == -1)
            return;
        if ((System.currentTimeMillis() - timestamp) < 250)
            v.requestFocus();
    }

    @Override public View getView (int position, View convertView, ViewGroup parent)
    {
        View v = mLayoutInflater.inflate(R.layout.mylayout, parent, false);

        EditText newText = (EditText) v.findViewById(R.id.email);
        if (mText != null)
            newText.setText(mText.getText());
        mText = newText;
        mText.setOnFocusChangeListener(this);
        reclaimFocus(mText, mTextLostFocusTimestamp);

        return v;
    }

    @Override public void onFocusChange(View v, boolean hasFocus) {
        if ((v == mText) && !hasFocus)
            mTextLostFocusTimestamp = System.currentTimeMillis();
    }
}
neteinstein

You should test this code on a device with hardware keyboard always visible. The behavior may also happen here.

To avoid this you can have the keyboard always visible.. but that is not very easy as you can see by this thread:

https://groups.google.com/forum/#!topic/android-developers/FyENeEdmYC0

Theoretically you may have to create your own Android keyboard (although using as base the stock Android keyboard) as described here: Android: How to make the keypad always visible?

In AndroidManifest.xml use adjustNothing in the activity that contain the views

<activity
            android:name=".ActivityName"
            android:windowSoftInputMode="adjustNothing">

If the editText inside the listView just make sure that you inflate the View in the getView method with this way.


        if (convertView == null)
        convertView = LayoutInflater.from(context).inflate(R.layout.yourItemListLayout,
                parent, false);   

Edit: this work for some mobiles not all I use the answer from Mr.Frank above.

Martin Stone

This guy had the same problem and more besides. He solved it by using a ScrollView and a LinearLayout instead of a ListView.

Add android:windowSoftInputMode="adjustResize" in the activity holding the listview or EditText. This will solve your problem.

<activity android:name=".MainActivity"
        android:windowSoftInputMode="adjustResize">
</activity>

For those who come here with Xamarin or Xamarin.Forms:

I had the same issue as well but only with Android 5.x - all newer Versions including 8.1 worked well.

Obviously sheltond was right by saying:

In my case, this is happening because when the ListView resizes, it re-creates all of the list items (i.e. it calls getView() again for each visible list item).

My listview was resizing as well and no, Franks solution to set windowSoftInputMode="adjustPan" was no option for me because that means that the keyboard moves the listview partly off the screen.

All I had to do after hours of focus-debugging was setting the cell caching strategy of the Xamarin Forms ListView:

From

CachingStrategy="RecycleElement"

To

CachingStrategy="RetainElement"

This will stop the cells from being recreated. However, this might result in bad performance and high memory consumption for huge lists. Be aware.

In my case, I had called root_scrollview.fullScroll(View.FOCUS_DOWN) on my root ScrollView when Keyboard appears. I replaced it with

login_scrollview.post(new Runnable() { 
    @Override
    public void run() {
        root_scrollview.scrollTo(0,root_container.bottom)
    }
});

where root_container is the immediate child of root_scrollview. This solved the problem for me.

Note: Directly calling root_scrollview.scrollTo(0,root_container.bottom) was not working.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!