EditText in Listview loses focus when pressed on Android 4.x

后端 未结 9 1173
孤城傲影
孤城傲影 2020-11-29 03:13

I know there are a lot of similar questions out here but I couldn\'t get any of the provided solutions working in a simple sample app.

The problem occurs when the

相关标签:
9条回答
  • 2020-11-29 03:45

    Modify your manifest xml to add windowSoftInputMode in your activity:

    <activity
        android:name=".YourActivity"
        android:windowSoftInputMode="adjustPan">
    </activity>
    
    0 讨论(0)
  • 2020-11-29 03:47

    Use recycler View, this solves several issues from list and gridview. Yo can even work with staggered gridviews. Yo could easily start working with this http://android-er.blogspot.com.co/2015/07/staggeredgridlayoutmanager-google-app.html

    0 讨论(0)
  • 2020-11-29 03:48

    I know its a very old thread but this answer might be helpful to someone so here it is:

    Switch to RecyclerView and you won't have to worry about these annoying issues of ListView. Instead of making new view it recycles and reuses old views.

    0 讨论(0)
  • 2020-11-29 03:49

    I was having problems with the ActionBar "stealing" focus when I pressed on an EditText located within a ListView row. The above solutions did not work, but the following solution worked for me:

    http://www.mysamplecode.com/2013/02/android-edittext-listview-loses-focus.html

    Basically I added this to my ListView:

    android:descendantFocusability="beforeDescendants"
    

    and added this to my activity:

    android:windowSoftInputMode="adjustPan"
    
    0 讨论(0)
  • 2020-11-29 03:51

    In my case I've added local value currentlyFocusedRow in my Adapter. In the method getView() I've add these code to each editText:

       if (currentlyFocusedRow == position) {
            editText.requestFocus();
       }
    
            editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        if (currentlyFocusedRow == -1) {
                            currentlyFocusedRow = position;
                        }
                    } else {
                        currentlyFocusedRow = -1;
                    }
                }
            });
    
    0 讨论(0)
  • 2020-11-29 03:56

    A classic hack for situations like this is to use a handler and postDelayed(). In your adapter:

    private int lastFocussedPosition = -1;
    private Handler handler = new Handler();
    
    public View getView(final int position, View convertView, ViewGroup parent) {
    
        // ...
    
        edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
    
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    handler.postDelayed(new Runnable() {
    
                        @Override
                        public void run() {
                            if (lastFocussedPosition == -1 || lastFocussedPosition == position) {
                                lastFocussedPosition = position;
                                edittext.requestFocus();
                            }
                        }
                    }, 200);
    
                } else {
                    lastFocussedPosition = -1;
                }
            }
        });
    
        return convertView;
    }
    

    This works on my device, but keep this code out of production. I also wouldn't be surprised if the focus bug manifests itself differently in different android versions or roms.

    There are also many other problems with embedding an EditText within a ListView that have solutions that feel like a hack. See all of the other people struggling.

    It's also very easy to have something like this happen:

    like this.

    After having gone down similar paths many times myself, I've mostly given up on trying to override any of the default keyboard behaviours or quirks. I would recommend trying to find alternative solution in your app if possible.

    Have you considered having the ListView rows be just a styled TextView and then displaying a Dialog with an EditText when a row is clicked, updating the TextView as necessary?

    0 讨论(0)
提交回复
热议问题