我有一个edittext和一个观察SPACE是否到达的textwatcher.如果它是一个空间我想立即删除它.或者,如果它是一个空间我想确保它不会出现但是以某种方式指示(seterror,toast)用户不允许空间.
edittext.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
//---//
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
Run Code Online (Sandbox Code Playgroud)
我无法在afterTextChaned方法中定义onkeydown,因为它给了我一个错误.
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
if (keyCode == KeyEvent.KEYCODE_SPACE) {
}
}
Run Code Online (Sandbox Code Playgroud)
所以它不起作用(语法错误,错误的构造为int keyCode.
提前谢谢你!
我有一个Edit Text数字只能介于两者之间0-59,没有其他数字会输入Edit Text,我试过text-watcher但没有成功.任何的想法?
12-26 14:59:39.715: E/AndroidRuntime(19494): FATAL EXCEPTION: main
12-26 14:59:39.715: E/AndroidRuntime(19494): java.lang.StackOverflowError
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494): at …Run Code Online (Sandbox Code Playgroud) 我实际上是在尝试进行卡片格式化,因为我正在尝试实现谷歌从链接中所说的内容
您没有被告知发生了更改的位置,因为其他afterTextChanged()方法可能已经进行了其他更改并使偏移无效.但是如果您需要在这里知道,可以在onTextChanged(CharSequence,int,int,int)中使用setSpan(Object,int,int,int)来标记您的位置,然后从此处查找跨度最终的位置.
从上面我所理解的是我需要在onTextChanged()中使用setSpan保存[CharSequence s,int start,int before,int count]并以某种方式在afterTextChanged()中检索它们.
问题是,在onTextChanged()中我在哪个对象上调用setSpan(),如何在afterTextChanged()中检索这些保存的值.
我正在使用此方法缩小TextView文本,因为它的名字暗示:
public static float shrinkTextToFit(String caller, float availableWidth, TextView textView, float startingTextSize, float minimumTextSize) {
startingTextSize = textView.getTextSize() < startingTextSize ? textView.getTextSize() : startingTextSize;
Log.i("123", "=========================");
Log.i("123", caller + " called shrinkTextToFit");
CharSequence text = textView.getText();
float textSize = startingTextSize;
textView.setTextSize(startingTextSize);
while (!TextUtils.equals(text, (TextUtils.ellipsize(text, textView.getPaint(), availableWidth, TextUtils.TruncateAt.END)))) {
textSize -= 2;
Log.i("123", "textSize: " + textSize);
if ((textSize <= minimumTextSize) || (textSize <= 0)) {
break;
} else {
textView.setTextSize(textSize);
}
}
return textSize;
}
Run Code Online (Sandbox Code Playgroud)
而且我只有这个设备有堆栈溢出(有时候不会发生):
我有一个ListView和一个EditText.我实现了addTextChangedListener对EditText以过滤的ListView内容.
leftList.setTextFilterEnabled(true);
et_search.addTextChangedListener(filterTextWatcher);
Run Code Online (Sandbox Code Playgroud)
然后TextWatcher是:
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (watcherAdapter==null) {
return;
}
watcherAdapter.getFilter().filter(s);
Log.e(TAG, "OnTextChange: " + s + " start: " + start +
" before: " + before + " count: …Run Code Online (Sandbox Code Playgroud) android filtering textwatcher android-listview android-edittext
所以我一直在尝试实现Android的TextWatcher并遇到一些问题,TextChangedListener被多次调用或进入无限循环,因为我想将EditText小部件中的文本转换为货币格式化的字符串.
我解决这个问题的方法是创建我自己的自定义TextWatcher然后在afterTextChanged事件中执行类似下面的操作
public class CurrencyTextWatcher implements TextWatcher {
private EditText et;
public CurrencyTextWatcher(EditText editText) {
et = editText;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
et.removeTextChangedListener(this);
et.setText(myCurrencyString);
et.addTextChangedListener(this);
}
}Run Code Online (Sandbox Code Playgroud)
所以我的问题是,有更好的方法吗?我希望有一个EditText Widget来保存编辑所在的位置以及生成的格式化字符串.
实际上还有其他任何问题,有关删除然后添加像这样的TextChangedListener?
提前致谢
我有一个带TextWatcher的EditText.它应该将文本格式化为人类高度,如5'9".但是当用户犯了错误并想删除输入错误的字符时,TextWather不允许他这样做.假设用户想删除"characther the TextWather立即加回.下面是代码.那么如何允许用户删除EditText中的文本?
private class CustomTextWatcher implements TextWatcher {
private EditText mEditText;
public CustomTextWatcher(EditText e) {
mEditText = e;
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
public void afterTextChanged(Editable s) {
int count = s.length();
String str = s.toString();
if (count == 1) {
str = str + "'";
} else if (count == 3) {
str = str + "\"";
} …Run Code Online (Sandbox Code Playgroud) 我正在使用这个带有标签的材料editText 库:https : //github.com/rey5137/Material/wiki/Text-Field nice库:)
但...
我正在使用下一个代码来检查输入的符号是否正确:
private boolean hasCorrectSymbols(String input){
String tre = "[A-Za-z0-9\\@\\#\\$\\%\\&\\*\\(\\)\\-\\+\\_\\;\\:\\?\\.\\,\\!]+$";
if (input.matches(tre)){
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
用于检查正确的符号,我正在使用textWatcher:
mEditPass.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() == 1 && !loginPassHasCorrectSymbols(s.toString())){
mEditPass.getText().clear();
String mess = getString(R.string.toast_login_useLatin);
showToastMessage(mess);
} else if (s.length() >1 && !loginPassHasCorrectSymbols(s.toString())) {
String mess = getString(R.string.toast_login_useLatin);
showToastMessage(mess);
String text = …Run Code Online (Sandbox Code Playgroud) android textwatcher android-edittext indexoutofboundsexception
我在EditText上实现TextWatcher,以在用户输入新字符时在文本内查找并加下划线的一系列关键字。但是,当在软键盘上选择建议的/自动完成的单词时,不是将建议的单词添加到Edittext,然后调用onTextChanged函数,将删除一半的完整单词。我发现这很奇怪,因为输入单个字符会激活onTextChanged函数。任何帮助将不胜感激。
PS。如果有人知道即时处理EditText的更好方法,请告诉我。
码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_codify_test);
final EditText editText = (EditText) findViewById(R.id.editText_codifyTest);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!checked) { //stop infinite loop
checked = true;
cursorPosition = editText.getSelectionStart(); //get cursor position before text modification
codifyText(editText);
} else {
checked = false;
}
}
@Override
public void afterTextChanged(Editable s) {
}
}); …Run Code Online (Sandbox Code Playgroud) android textwatcher android-softkeyboard android-edittext android-textwatcher
我有一个从 Android 继承的接口,TextWatcher仅用于实现afterTextChanged方法。我在我的项目中启用了 Java 8 支持,并在build.gradle文件中添加了源和目标兼容性选项,但即使它在调试版本中运行完美,它在我测试的每台设备上的发布版本中都失败了。我首先在 Play Console 的预发布报告中注意到了它,并在 Firebase 的测试实验室中再次进行了测试,但仍然是每次设备抛出AbstractMethodError后都会崩溃。
这是我的AfterTextChangedListener:
import android.text.Editable;
import android.text.TextWatcher;
public interface AfterTextChangedListener extends TextWatcher {
@Override
default void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Do nothing
}
@Override
default void onTextChanged(CharSequence s, int start, int before, int count) {
// Do nothing
}
@Override
void afterTextChanged(Editable s);
}
Run Code Online (Sandbox Code Playgroud)
这是使用此接口的代码部分:
mSomeEditText.addTextChangedListener((AfterTextChangedListener) editable -> {
// Logic using 'editable'.
});
Run Code Online (Sandbox Code Playgroud)
这是崩溃的 …