如何使用 TextWatcher 在 EditText 上获取新输入的值
How can I get new entered value on EditText with TextWatcher
我可以得到旧值。但我没有解决方案来获得新的输入
值。
其实,我想把旧值和新值分开。
例如:如果 oldText=hello
和新输入的 EditText 值等于(hello w
或 w hello
),我想要 newText=w
.
public class MyTextWatcher implements TextWatcher {
private String oldText = "";
private String newText = "";
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
this.oldText = s.toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
}
感谢您的帮助。
试试这个代码:
public class MyTextWatcher implements TextWatcher {
private String oldText = "";
private String newText = "";
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
this.oldText = s.toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
newText = s.toString().replace(oldText, "").trim();
}
@Override
public void afterTextChanged(Editable s) {
}
}
关于文本观察器的信息:
Differences between TextWatcher 's onTextChanged, beforeTextChanged and afterTextChanged
通过start
和count
参数在onTextChanged
方法中,可以计算得到新的输入值。
This method is called to notify you that, within s
, the count
characters beginning at start
have just replaced old text that had
length before
. It is an error to attempt to make changes to s
from
this callback.
所以你可以:
public class MyTextWatcher implements TextWatcher {
private String newTypedString = "";
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
newTypedString = s.subSequence(start, start + count).toString().trim();
}
@Override
public void afterTextChanged(Editable s) {
}
}
我可以得到旧值。但我没有解决方案来获得新的输入 值。
其实,我想把旧值和新值分开。
例如:如果 oldText=hello
和新输入的 EditText 值等于(hello w
或 w hello
),我想要 newText=w
.
public class MyTextWatcher implements TextWatcher {
private String oldText = "";
private String newText = "";
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
this.oldText = s.toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
}
感谢您的帮助。
试试这个代码:
public class MyTextWatcher implements TextWatcher {
private String oldText = "";
private String newText = "";
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
this.oldText = s.toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
newText = s.toString().replace(oldText, "").trim();
}
@Override
public void afterTextChanged(Editable s) {
}
}
关于文本观察器的信息: Differences between TextWatcher 's onTextChanged, beforeTextChanged and afterTextChanged
通过start
和count
参数在onTextChanged
方法中,可以计算得到新的输入值。
This method is called to notify you that, within
s
, thecount
characters beginning atstart
have just replaced old text that had lengthbefore
. It is an error to attempt to make changes tos
from this callback.
所以你可以:
public class MyTextWatcher implements TextWatcher {
private String newTypedString = "";
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
newTypedString = s.subSequence(start, start + count).toString().trim();
}
@Override
public void afterTextChanged(Editable s) {
}
}