Select edittexts text in a listview. How can be done? I can't find out

后端 未结 6 1601
不知归路
不知归路 2021-01-06 10:07

I have a ListView with an EditText on each row working.

I need to select this text on click the edittext to write numbers without erasing or moving the cursor.

6条回答
  •  逝去的感伤
    2021-01-06 10:44

    Not sure if you are still looking for a solution, but I found a way to do it. Add a focus change listener to the edittext in the getView method of your adapter and select all text when focus is received.

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        // blah blah blah
    
        EditText edit = (EditText) view.findViewById(R.id.editTextId);
        edit.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus)
                    ((EditText) view).selectAll();
            }
        });
    
        return view;
    }
    

    I had set windowSoftInputMode to adjustPan for the activity, although I don't think it has any bearing on this matter.

    I have tested this on Android 2.3 and 4 and it works.

提交回复
热议问题