- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用一个简单的库来进行撤消和重做并且效果很好,但唯一的问题是当我在获取所有已删除文本的末尾不断单击撤消按钮时,它会清除我的所有文本。我不想清除我的所有文本,而是在通过单击撤消按钮获取所有文本后我希望撤消按钮禁用。
这是我正在使用的用于撤消和重做的 java
public class TextViewUndoRedo {
/**
* Is undo/redo being performed? This member signals if an undo/redo
* operation is currently being performed. Changes in the text during
* undo/redo are not recorded because it would mess up the undo history.
*/
private boolean mIsUndoOrRedo = false;
/**
* The edit history.
*/
private EditHistory mEditHistory;
/**
* The change listener.
*/
private EditTextChangeListener mChangeListener;
/**
* The edit text.
*/
private TextView mTextView;
// =================================================================== //
/**
* Create a new TextViewUndoRedo and attach it to the specified TextView.
*
* @param textView
* The text view for which the undo/redo is implemented.
*/
public TextViewUndoRedo(TextView textView) {
mTextView = textView;
mEditHistory = new EditHistory();
mChangeListener = new EditTextChangeListener();
mTextView.addTextChangedListener(mChangeListener);
}
// =================================================================== //
/**
* Disconnect this undo/redo from the text view.
*/
public void disconnect() {
mTextView.removeTextChangedListener(mChangeListener);
}
/**
* Set the maximum history size. If size is negative, then history size is
* only limited by the device memory.
*/
public void setMaxHistorySize(int maxHistorySize) {
mEditHistory.setMaxHistorySize(maxHistorySize);
}
/**
* Clear history.
*/
/* public void clearHistory() {
mEditHistory.clear();
}*/
/**
* Can undo be performed?
*/
public boolean getCanUndo() {
return (mEditHistory.mmPosition > 0);
}
/**
* Perform undo.
*/
public void undo() {
EditItem edit = mEditHistory.getPrevious();
if (edit == null) {
return;
}
Editable text = mTextView.getEditableText();
int start = edit.mmStart;
int end = start + (edit.mmAfter != null ? edit.mmAfter.length() : 0);
mIsUndoOrRedo = true;
text.replace(start, end, edit.mmBefore);
mIsUndoOrRedo = false;
// This will get rid of underlines inserted when editor tries to come
// up with a suggestion.
for (Object o : text.getSpans(0, text.length(), UnderlineSpan.class)) {
text.removeSpan(o);
}
Selection.setSelection(text, edit.mmBefore == null ? start
: (start + edit.mmBefore.length()));
}
/**
* Can redo be performed?
*/
public boolean getCanRedo() {
return (mEditHistory.mmPosition < mEditHistory.mmHistory.size());
}
/**
* Perform redo.
*/
public void redo() {
EditItem edit = mEditHistory.getNext();
if (edit == null) {
return;
}
Editable text = mTextView.getEditableText();
int start = edit.mmStart;
int end = start + (edit.mmBefore != null ? edit.mmBefore.length() : 0);
mIsUndoOrRedo = true;
text.replace(start, end, edit.mmAfter);
mIsUndoOrRedo = false;
// This will get rid of underlines inserted when editor tries to come
// up with a suggestion.
for (Object o : text.getSpans(0, text.length(), UnderlineSpan.class)) {
text.removeSpan(o);
}
Selection.setSelection(text, edit.mmAfter == null ? start
: (start + edit.mmAfter.length()));
}
/**
* Store preferences.
*/
public void storePersistentState(Editor editor, String prefix) {
// Store hash code of text in the editor so that we can check if the
// editor contents has changed.
editor.putString(prefix + ".hash",
String.valueOf(mTextView.getText().toString().hashCode()));
editor.putInt(prefix + ".maxSize", mEditHistory.mmMaxHistorySize);
editor.putInt(prefix + ".position", mEditHistory.mmPosition);
editor.putInt(prefix + ".size", mEditHistory.mmHistory.size());
int i = 0;
for (EditItem ei : mEditHistory.mmHistory) {
String pre = prefix + "." + i;
editor.putInt(pre + ".start", ei.mmStart);
editor.putString(pre + ".before", ei.mmBefore.toString());
editor.putString(pre + ".after", ei.mmAfter.toString());
i++;
}
}
/**
* Restore preferences.
*
* @param prefix
* The preference key prefix used when state was stored.
* @return did restore succeed? If this is false, the undo history will be
* empty.
*/
public boolean restorePersistentState(SharedPreferences sp, String prefix)
throws IllegalStateException {
boolean ok = doRestorePersistentState(sp, prefix);
if (!ok) {
mEditHistory.clear();
}
return ok;
}
private boolean doRestorePersistentState(SharedPreferences sp, String prefix) {
String hash = sp.getString(prefix + ".hash", null);
if (hash == null) {
// No state to be restored.
return true;
}
if (Integer.valueOf(hash) != mTextView.getText().toString().hashCode()) {
return false;
}
mEditHistory.clear();
mEditHistory.mmMaxHistorySize = sp.getInt(prefix + ".maxSize", -1);
int count = sp.getInt(prefix + ".size", -1);
if (count == -1) {
return false;
}
for (int i = 0; i < count; i++) {
String pre = prefix + "." + i;
int start = sp.getInt(pre + ".start", -1);
String before = sp.getString(pre + ".before", null);
String after = sp.getString(pre + ".after", null);
if (start == -1 || before == null || after == null) {
return false;
}
mEditHistory.add(new EditItem(start, before, after));
}
mEditHistory.mmPosition = sp.getInt(prefix + ".position", -1);
if (mEditHistory.mmPosition == -1) {
return false;
}
return true;
}
// =================================================================== //
/**
* Keeps track of all the edit history of a text.
*/
private final class EditHistory {
/**
* The position from which an EditItem will be retrieved when getNext()
* is called. If getPrevious() has not been called, this has the same
* value as mmHistory.size().
*/
private int mmPosition = 0;
/**
* Maximum undo history size.
*/
private int mmMaxHistorySize = -1;
/**
* The list of edits in chronological order.
*/
private final LinkedList<EditItem> mmHistory = new LinkedList<EditItem>();
/**
* Clear history.
*/
private void clear() {
mmPosition = 0;
mmHistory.clear();
}
/**
* Adds a new edit operation to the history at the current position. If
* executed after a call to getPrevious() removes all the future history
* (elements with positions >= current history position).
*/
private void add(EditItem item) {
while (mmHistory.size() > mmPosition) {
mmHistory.removeLast();
}
mmHistory.add(item);
mmPosition++;
if (mmMaxHistorySize >= 0) {
trimHistory();
}
}
/**
* Set the maximum history size. If size is negative, then history size
* is only limited by the device memory.
*/
private void setMaxHistorySize(int maxHistorySize) {
mmMaxHistorySize = maxHistorySize;
if (mmMaxHistorySize >= 0) {
trimHistory();
}
}
/**
* Trim history when it exceeds max history size.
*/
private void trimHistory() {
while (mmHistory.size() > mmMaxHistorySize) {
mmHistory.removeFirst();
mmPosition--;
}
if (mmPosition < 0) {
mmPosition = 0;
}
}
/**
* Traverses the history backward by one position, returns and item at
* that position.
*/
private EditItem getPrevious() {
if (mmPosition == 0) {
return null;
}
mmPosition--;
return mmHistory.get(mmPosition);
}
/**
* Traverses the history forward by one position, returns and item at
* that position.
*/
private EditItem getNext() {
if (mmPosition >= mmHistory.size()) {
return null;
}
EditItem item = mmHistory.get(mmPosition);
mmPosition++;
return item;
}
}
/**
* Represents the changes performed by a single edit operation.
*/
private final class EditItem {
private final int mmStart;
private final CharSequence mmBefore;
private final CharSequence mmAfter;
/**
* Constructs EditItem of a modification that was applied at position
* start and replaced CharSequence before with CharSequence after.
*/
public EditItem(int start, CharSequence before, CharSequence after) {
mmStart = start;
mmBefore = before;
mmAfter = after;
}
}
/**
* Class that listens to changes in the text.
*/
private final class EditTextChangeListener implements TextWatcher {
/**
* The text that will be removed by the change event.
*/
private CharSequence mBeforeChange;
/**
* The text that was inserted by the change event.
*/
private CharSequence mAfterChange;
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
if (mIsUndoOrRedo) {
return;
}
mBeforeChange = s.subSequence(start, start + count);
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (mIsUndoOrRedo) {
return;
}
mAfterChange = s.subSequence(start, start + count);
mEditHistory.add(new EditItem(start, mBeforeChange, mAfterChange));
}
public void afterTextChanged(Editable s) {
}
}
}
还有我的按钮和 Activity
switch (v.getId()) {
case R.id.undo:
mTextViewUndoRedo.undo();
break;
case R.id.redo:
mTextViewUndoRedo.redo();
break;
最佳答案
你的撤消工作应该在最后阶段之前返回。
public boolean getCanUndo() {
return (mEditHistory.mmPosition > 1);
}
关于java - 撤消所有文本时如何禁用按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53587299/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!