Select the text edittexts in the list. How can I do that? I can not find out

I have a ListView with EditText for every row that works.

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

The selectAllonfocus function is used first. However, after scrolling through the list, EditText went crazy and the selection did not work correctly.

If I execute selectAll in the onFocus listener, then when I click, a context menu appears (select a word, select all, etc.) instead of selecting.

If anyone can help.

Thanks.

+4
source share
6 answers

What I can say about what I asked is that trying to select all the text of the edittexts lists does not work properly. Therefore, instead of selecting text, I show a dialog in which the user selects a number or something else.

0
source

I can’t say exactly what you are trying to do. Perhaps this will help if you can publish some of your respective source codes ...

But the fact that it is crazy when you start scrolling makes me realize that you are not processing convertView correctly in your getView () method.

+2
source

If you have problems after scrolling, this is a recycling view that will launch you. You basically need to set up an array to hold the contents of EditTexts, so when scrolling they will not get mixed up. You did not say what your list supports, but I have a list with edittexts contained here, and here is how I process it (from the cursor, but you can adapt it for the ArrayAdapter):

public class CursorAdapter_EditText extends SimpleCursorAdapter { private static Cursor c; private Context context; public static String[] quantity; private int layout; public CursorAdapter_EditText(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); CursorAdapter_EditText.c = c; this.context = context; this.layout = layout; initializeQuantity(); // Call method to initialize array to hold edittext info } public static void initializeQuantity() { quantity = new String[c.getCount()]; // Initialize array to proper # of items int i = 0; while (i < c.getCount()) { quantity[i] = ""; // set all EditTexts to empty i++; } } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) convertView = View.inflate(context, layout, null); final int pos = position; View row = convertView; c.moveToPosition(position); TextView name = (TextView) row.findViewById(R.id.ListItem1); EditText qty = (EditText) row.findViewById(R.id.qty); qty.setOnFocusChangeListener(new View.OnFocusChangeListener() { // Set so EditText will be saved to array when you leave it @Override public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus) { LinearLayout parent = (LinearLayout) v.getParent(); EditText qtyTemp = (EditText) parent.findViewById(R.id.qty); // Get a reference to EditText (you could probaly use v here) quantity[pos] = qtyTemp.getText().toString(); // Save contents of EditText to array } } }); name.setText(c.getString(1)); unit.setText(c.getString(3)); qty.setText(quantity[position]); return (row); } } 

Then I have a button outside the array that processes it back to my database as follows:

 commit.setOnClickListener(new OnClickListener() { public void onClick(View v) { int i = 0; itemCursor.moveToFirst(); while (itemCursor.isAfterLast() == false) { if (CursorAdapter_EditText.quantity[i].equals("")) { CursorAdapter_EditText.quantity[i] = "0"; } ; int tempQty = Integer .parseInt(CursorAdapter_EditText.quantity[i]); if (tempQty != 0) { mDbHelper.createListItem(listId, itemCursor .getInt(itemCursor .getColumnIndex(GroceryDB.ITEM_ROWID)), tempQty, 0); } i++; itemCursor.moveToNext(); } } }); 
+1
source

I encountered the same problem by following these steps:

  • Remove android: descendantFocusability = "beforeDescendants" from xml with ListView, android: windowSoftInputMode = "adjustPan" from AndroidManifest - they do not help.
  • Subclasses of 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 works very well, this is not the code I'm proud of ... If someone knows a beautiful solution, please tell me!

+1
source

In the getView method of your adapter, get your EditView (in this code, edittxt) and run:

  edittxt.post(new Runnable() { public void run() { edittxt.requestFocusFromTouch(); edittxt.selectAll(); InputMethodManager lManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); lManager.showSoftInput(edittxt, 0); } }); 
+1
source

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

 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 set windowSoftInputMode to configure Pan for activity, although I don't think this has anything to do with this issue.

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

0
source

Source: https://habr.com/ru/post/1413846/


All Articles