如何在Android中单击按钮上的EditText上应用撤消和重做操作 [英] How to apply Undo and Redo operation to EditText on Button click in android
问题描述
我正在尝试在对EditText进行写入或应用效果时应用撤消"和重做"操作.为此,我已经从链接下载了课程,然后我已经在我的应用程序中使用过它.
I am trying to apply Undo and Redo operation while writing or applying effect to my EditText. For that i have downloaded class from this Link and then i have used it like this in my app.
撤消
TextViewUndoRedo mTextViewUndoRedo = new TextViewUndoRedo(edtNoteDescription);
mTextViewUndoRedo.undo();
重做
TextViewUndoRedo mTextViewUndoRedo = new TextViewUndoRedo(edtNoteDescription);
mTextViewUndoRedo.redo();
但是我不知道为什么这段代码不起作用,我放了日志,检查是否调用了Undo函数,不幸的是,我看到它正在调用此函数,但是它位于下面的方法中.
But i don't know why this code does not work, I have put the log and check whether the function Undo is called or not and unfortunately i saw that it is calling this function but it goes inside below method.
if (edit == null) {
return;
}
我也尝试了其他一些没有运气的解决方案,因此,如果有人用此方法或任何其他方法实现了相同的解决方案,那么请建议一些实现此功能的代码或方法.
I have also tried with some other solution with no luck, So if anyone who has implemented the same with this method or with any other method then please do suggest some code or way to implement this functionality.
修改
btnUndo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextViewUndoRedo mTextViewUndoRedo = new TextViewUndoRedo(edtNoteDescription);
mTextViewUndoRedo.undo();
}
});
推荐答案
问题可能出在您每次单击按钮时都在创建TextViewUndoRedo
对象吗?
Could the problem be that you are creating the TextViewUndoRedo
object each time the button is clicked?
这就是为什么EditHistory
为空的原因,因为每次都会重新创建它.
这行不行吗?
That is why EditHistory
is empty cause it's getting recreated each time.
Wouldn't this work?
TextViewUndoRedo mTextViewUndoRedo = new TextViewUndoRedo(edtNoteDescription);
btnUndo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mTextViewUndoRedo.undo();
}
});
就像我在评论中说的那样,Undo()
方法调用mEditHistory.getPrevious()
,但是getPrevious()
返回null
,因为它在其中执行:
Like I said in the comments, The Undo()
method calls mEditHistory.getPrevious()
but getPrevious()
returns null
because inside of it, it executes:
if (mmPosition == 0) {
return null;
}
创建TextViewUndoRedo
时,将创建一个新的EditHistory
并将其内部的mmPosition
初始化为0.
由于您每次都在重新创建对象,因此mmPosition
始终为0,并且您会得到null
.
When TextViewUndoRedo
is created, a new EditHistory
is created and inside of it mmPosition
is initialized to 0.
Since you are re-creating the object each time, mmPosition
is always 0 and you get null
back.
这篇关于如何在Android中单击按钮上的EditText上应用撤消和重做操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!