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

后端 未结 6 1699
不知归路
不知归路 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:24

    I dealt with exactly the same problem by doing the following:

    1. Remove android:descendantFocusability="beforeDescendants" from xml with ListView, android:windowSoftInputMode="adjustPan" from AndroidManifest - they ain't help.
    2. Subclassed EditText

      public class TrickyEditText extends EditText {
      
          private boolean mFocused = false;
          private boolean mTouched = false;
      
          public TrickyEditText(Context context) {
              super(context);
          }
      
          public TrickyEditText(Context context, AttributeSet attrs) {
              super(context, attrs);
          }
      
          public TrickyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
              super(context, attrs, defStyleAttr);
          }
      
          @TargetApi(Build.VERSION_CODES.LOLLIPOP)
          public TrickyEditText(Context context, AttributeSet attrs, int defStyleAttr, int style) {
              super(context, attrs, defStyleAttr, style);
          }
      
          @Override
          public boolean didTouchFocusSelect() {
              if (mTouched && mFocused) {
                  return true;
              } else {
                  return super.didTouchFocusSelect();
              }
          }
      
          @Override
          public boolean onTouchEvent(MotionEvent event) {
              mTouched = true;
              return super.onTouchEvent(event);           
          }
      
          @Override
          protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
              mFocused = focused;
              if (!focused) {
                  mTouched = false;
              }
              super.onFocusChanged(focused, direction, previouslyFocusedRect);
          }
      }
      
      1. Adapter code

        public class TrickyAdapter extends ArrayAdapter {
        
            .............
        
            @Override
            public View getView(int childPosition, View convertView, final ViewGroup parent) {
        
                .........
        
                TrickyEditText et = ..... //initialize edittext
        
                et.setOnFocusChangeListener(new OnFocusChangeListener() {
        
                    @Override
                    public void onFocusChange(final View v, boolean hasFocus) {
                        if (hasFocus) {
                            v.post(new Runnable() {
        
                                @Override
                                public void run() {
                                    ((EditText)v).selectAll();
                                }
                            });                    
                        }
                    }
                });
        
                .........
            }
        }
        

    Although it's working pretty well, it's not the code I'm proud of... If somebody knows a prettier solution, please tell me!

提交回复
热议问题