How to perform an undo operation in EditText

I want to know if there is any method or any link or tutorial for performing an undo operation in an android edittext. If anyone knows than please let me know.

+4
source share
5 answers

There is an undo / redo implementation for Android EditText at http://credentiality-android-scripting.googlecode.com/hg/android/ScriptingLayerForAndroid/src/com/googlecode/android_scripting/activity/ScriptEditor.java

The code works, but it does not handle configuration changes correctly. I am working on a fix and will post it here when it is complete.

My google search was: - android edittext onTextChanged undo

+3
source

Quick note about Antti-Brax / Divers (Kidinov ). It works great if you try to use it with the PostView API 23 text-based interface, you will have problems because guess that Google actually added a hidden UndoManager (android.content.UndoManager) and did not document it or made it not obvious that it was there. But if you have a hard / bluetooth keyboard in Marshmallow or Nougat and press ^ Z or SHIFT- ^ Z, you will get undo / redo.

The problem arises if you already use the Antti-Brax class with EditText, and you also bind it to ^ Z and shift- ^ Z, you will run into problems with someone using a hard keyboard. Namely: ^ Z will call BOTH native and Antti-Brax undo, which will lead to two cancellations at the same time, which is not very good. And after a few of them, you are likely to get a crash outside.

A possible solution I found is to subclass TextView / TextEdit / whatever and intercept the cancel / redo calls from TextView so that they don't start as follows:

@Override public boolean onTextContextMenuItem(int id) { int ID_UNDO, ID_REDO; try { ID_UNDO = android.R.id.undo; ID_REDO = android.R.id.redo; } catch (Resources.NotFoundException e) { ID_UNDO = 16908338; // 0x1020032 ID_REDO = 16908339; // 0x1020033 } return !((id == ID_UNDO) || (id == ID_REDO)) && super.onTextContextMenuItem(id); } 

These magic identification numbers were found here and are used only as a backup if the values โ€‹โ€‹of android.R.id.undo are not found. (it may also be reasonable to assume that if there are no values, the function is not there, but in any case ...)

This is not the best solution, because both tracker cancellations are still there, and both work in the background. But at least you wonโ€™t run them both at the same time with Z. This is the best I could think of until it is officially documented and the getUndoManager () methods of TextView are no longer hidden ...

Why they made a function that you cannot turn off (or even know whether it was there or not) "live" in the released Android, I canโ€™t say.

I just opened an issue on an Android tracker if someone wants to follow this.

+2
source

Here is the undo / redo implementation related to Gary Phillips' answer, extracted into a reusable and universal undo / redo plugin for any widget that descends from TextView. I added code to save the cancellation history.

http://code.google.com/p/android/issues/detail?id=6458#c123

Hope this helps.

+1
source

Saving the EditText style for cancellation: You can create an ArrayList<EditText> or ArrayList<String> (String containing html text) to save your last 10 (for example) actions. So, ArrayList [0] will contain the html text from your first action, and ArrayList [9] will contain the html text from your last action. Each time a user selects a "cancel" in your application, you apply an ArrayList [size()-1] to your EditText, and then remove the ArrayList [size () - 1] from your array.

+1
source

I know this is an old question, but since there is no answer, and this is a problem that I have encountered with many angles, I would like to add my solution if it helps someone. My answer is probably most appropriate for large (1000 words +) volumes of text editing applications that require this feature.

The easiest way to solve this problem is to make periodic copies of all the text on the screen, save it in an array, and call setText() every time the undo method is called. This makes a reliable system, but it is not ideal for large (i.e. 1000 words +) text editing applications. This is because he:

  • Wasteful. Perhaps only one word is changed in a document by two thousand words, so that one thousand nine hundred and ninety-nine words are uselessly transferred to memory.
  • It can lead to performance problems, as some low-level hardware struggles with rendering large amounts of text. On some of my test devices, this method may crash for several seconds when calling Undo.

The solution I'm using right now is relatively complex, but I posted the results in here .

Essentially, this library saves a copy of the text as soon as the user starts typing, and then another copy of the text as soon as they stop printing for a given amount of time (in my case, two seconds). Then, two text lines and the changed section of the text are compared, the indices in which the changes occurred, as well as information about whether the change was adding new text, deleting or replacing the old text with new text,

The end result is that only the necessary text is saved, and when Undo is called, there is only a local call to delete() , replace() or insert() , which does much faster operations on large text fields.

+1
source

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


All Articles