我必须做一些明显的事情,但我无法弄清楚它是什么.我只是想在可编辑中插入一个字符:
@Override
public void afterTextChanged(Editable s) {
Log.d(TAG, "inserting space at " + location);
s.insert(location, " ");
Log.d(TAG, "new word: '" + s + "'");
}
Run Code Online (Sandbox Code Playgroud)
但是永远不会改变.字符串's'足够长,因为我打印它看起来很好.如果我调用Editable.clear(),它将被清除,我可以用Editable.replace()替换多个字符.想法?
我在Android上工作.以前我曾经onKeyListener处理过关键事件的具体行动.
但是,这种方式似乎不能解决我的问题,因为一旦我实现了我的监听器,几乎所有的密钥都会被禁用EditText.在SO阅读一些话题之后,我知道我应该使用TextWatcher代替,但我仍然不知道如何处理ENTER内部,因为参数提供了关键的事件,只有CharSequence,Editable等我没有找到任何keyCode参数.
我有一个EditText过滤ListView下面的项目,通常可能包含超过1000个项目.该TextWatcher方法是:
txt_itemSearch.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
fillItemList();
}
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
});
Run Code Online (Sandbox Code Playgroud)
这里的问题是,对于用户键入的每个字母,列表都会刷新,这是重复列表更新,导致UI变慢.
如何TextWatcher等待1-2秒,如果2秒后没有更多输入,则过滤列表.有什么建议吗?
而不是5118710,它应该是511-8710.我想在用户输入EditText中已有3位数的用户之后添加一个破折号.EditText的最大长度仅为7位数.
在我弄清楚上述问题之后,我再次陷入编码困境.当我已经输入3位数字时,它会附加破折号(这就是我想要发生的事情),但我的问题是接下来的3位数字也会附加破折号(如下所示511-871-) ...请帮助我.谢谢!
txt_HomeNo.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
boolean flag = true;
String eachBlock[] = txt_HomeNo.getText().toString().split("-");
for (int i = 0; i < eachBlock.length; i++) {
if (eachBlock[i].length() > 3) {
flag = false;
}
}
if (flag) {
txt_HomeNo.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL)
keyDel = 1;
return false;
}
});
if (keyDel …Run Code Online (Sandbox Code Playgroud) TextView textView=new TextView(context);
textView.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
s.append("A");
}
});
Run Code Online (Sandbox Code Playgroud)
如果我们添加一个TextWatchera TextView,并且我想在其中附加一个字母TextView,每次用户在其中写一个字母,但这会不断重新调用TextWatcherListener,依此类推StackOverFlow error,所以如何附加一个文本而不重新打电话给TextWatcher听众?
我正在使用TextWatcher,我无法检测事件中的Backspace密钥TextWatcher.afterTextChange.我还希望textView在某些情况下明确textWatcher事件.
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
// I want to detect backspace key here
}
Run Code Online (Sandbox Code Playgroud) 我有一个edittext,它只获得没有十进制数字的数字.
android:inputType="number"
Run Code Online (Sandbox Code Playgroud)
我打算在打字的时候把数千人分开.例如25,000.
我知道我应该使用TextWatcher并且我已经使用过这段代码,但是我无法使用它:
@Override
public void afterTextChanged(Editable viewss) {
String s = null;
try {
// The comma in the format specifier does the trick
s = String.format("%,d", Long.parseLong(viewss.toString()));
} catch (NumberFormatException e) {
}
}
Run Code Online (Sandbox Code Playgroud)
你能帮我这么做吗?
我正在尝试使用PhoneNumberFormattingTextWatcher,但没有任何事情发生,好像代码不存在
这是代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
EditText PhoneEdit = (EditText) findViewById(R.id.editText1);
PhoneEdit.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
}
Run Code Online (Sandbox Code Playgroud)
输入002010555666时它仍然是相同的no - 或+或(),只是相同而没有任何格式化代码中缺少任何内容
救命
我实际上是在尝试进行卡片格式化,因为我正在尝试实现谷歌从链接中所说的内容
您没有被告知发生了更改的位置,因为其他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)
而且我只有这个设备有堆栈溢出(有时候不会发生):