Android edittext in listview loses focus on callin

2019-02-06 03:14发布

I have a few edittext within a listview. i have a generic focuslistener on the edittext that updates the value of the data model and also the background of the edittext when focus is lost by calling notifydatachanged . The problem is that if one of the edittext is holding focus, when i touch the next edittext, it gains focus momentarily then loses focus. I suspect it is due to the notifydatachanged method call that is causing all views to be redrawn, after which the focus is lost. Does anyone have a suggestion or work around on the issue? Thanks.

5条回答
小情绪 Triste *
2楼-- · 2019-02-06 03:30

I also had a problem of EditText loosing the focus, but calling setDescendantFocusability(ListView.FOCUS_AFTER_DESCENDANTS) solved it.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    final ListView lv = getListView();
    lv.setDescendantFocusability(ListView.FOCUS_AFTER_DESCENDANTS);
}
查看更多
Explosion°爆炸
3楼-- · 2019-02-06 03:33

Example of implementing Sam Judd's suggestion with an additional switch use_last_focus

public class my_friends_adapter extends ArrayAdapter<group_user> {
    boolean use_last_focus=false;
    int currentlyFocusedRow=-1;
    String currentlyFocusedField="";

    public my_friends_adapter(Context context, int resource, ArrayList<group_user> users) {
            super(context, resource, users);
              }
        EditText tvName,tvEmail;

    @Override
   public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) convertView = LayoutInflater.from(getContext()).inflate(R.layout.my_friends_item, parent, false);
        tvName = (EditText) convertView.findViewById(R.id.tvName);
        tvEmail = (EditText) convertView.findViewById(R.id.tvEmail);
        if (use_last_focus && currentlyFocusedRow ==position){
            if (currentlyFocusedField=="tvName")tvName.requestFocus();
            else tvEmail.requestFocus();
            use_last_focus=false;
        }
        tvName.setTag(position);//When focus is lost save the entered value for later use
        tvName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {save_data2( v,  hasFocus);
            }
        });

        tvEmail.setTag(position);//When focus is lost save the entered value for later use
        tvEmail.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {save_data2( v,  hasFocus);}
        });

        return convertView;
        }


    public  void save_data2(View v, boolean hasFocus){
        int position = (Integer) v.getTag();
        EditText tvName1 = (EditText) v.findViewById(R.id.tvName);
        EditText tvEmail1 = (EditText) v.findViewById(R.id.tvEmail);
        boolean data_changed=false;
        if (hasFocus) currentlyFocusedRow=position;
        if (!(tvName1==null)) {
            if (hasFocus) currentlyFocusedField="tvName";
        }
        if (!(tvEmail1==null)) {
            if (hasFocus) currentlyFocusedField="tvEmail";
        }
        // set when data_changed
        // ..........
        if (!hasFocus && data_changed) {
            //your code to save the changed data
            // ...............
            //
            my_friends.adapter.notifyDataSetChanged();
            use_last_focus=true;
        }

    }



}
查看更多
淡お忘
4楼-- · 2019-02-06 03:40

It is indeed happening because all the views are redrawn, so the edit text representing whatever row used to be focused is now a completely different object. Set a variable in your adapter: int currentlyFocusedRow;

in getView for your adapter: Add an onFocusChanged listener to each edit text and when that edit text gains focus, set currentlyFocusedRow = whatever row the focused edit text happens to be in. Also set any edit text that is in the currentlyFocusedRow to be focused.

查看更多
我只想做你的唯一
5楼-- · 2019-02-06 03:42

For future users, everything here is overcomplicated. The correct answer is to add

android:descendantFocusability="afterDescendants"

to your listview.

查看更多
我命由我不由天
6楼-- · 2019-02-06 03:49

Set

android:windowSoftInputMode="adjustPan"

for your activity in the AndroidManifest.xml

查看更多
登录 后发表回答