TextChangedListener - onTextChanged,beforeTextChanged和afterTextChanged之间的差异 [英] TextChangedListener - Differences between onTextChanged, beforeTextChanged and afterTextChanged
问题描述
在我的安卓项目我不得不添加的 TextChangedListener ,以一个编辑文本查看。这样来说,在这三个部分。
In my Android Project I have had to add a TextChangedListener to a edit text view. And there are three parts in it.
- onTextChanged
- beforeTextChanged
- afterTextChanged
什么是这三者的型动物。我不得不实施的关键lisner和我的情况下,所有这三个看起来一样搜索的表。此外,他们发挥作用是相同的。当我输入一个产品名称的一部分的表重绘只有那些产品包含在其输入的文本。但是我用了afterTextChanged一部分。我的code是
What are the differents of these three. I have had to implement a search of a table on the key lisner and for my case all these three looks the same. Also they functioned the same. When I input a part of a product name the table redraws with only those products contains that entered text in it. But I used the afterTextChanged part. My code is
EditProduct.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
// System.out.println("onTextChanged"+s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
// System.out.println("beforeTextChanged"+s);
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
// System.out.println("afterTextChanged"+s);
String new_prx = s.toString();
System.out.println(s);
mini_productList = new ArrayList<Product>();
// mini_productList
int count = 0;
if (new_prx.equals("")) {
loadtableProducts(productList);
} else {
for (int i = 0; i < productList.size(); i++) {
if (productList.get(i).getDescription().toString()
.substring(0, (new_prx.length()))
.equalsIgnoreCase(new_prx)) {
mini_productList.add(productList.get(i));
count++;
}
}
loadtableProducts(mini_productList);
}
}
});
所以,有人可以给我这三个的explenation。谢谢!!!!
So can someone give me an explenation on these three. Thank you!!!!
推荐答案
onTextChanged
文本变化过程中运行。
onTextChanged
runs during the text changing.
afterTextChanged
立即运行文本更改后。
afterTextChanged
runs immediately after the text is changed.
beforeTextChanged
运行文本更改前的瞬间。
beforeTextChanged
runs the instant before the text is changed.
根据当你想要分配变量或做的事情上,你可能需要运行C的瞬间$ C $的变化之前,或之后瞬间
Depending on when you want to assign variables or do things, you may want to run the code the instant before the change, or the instant after.
下面是这样一个例子:
String afterTextChanged = "";
String beforeTextChanged = "";
String onTextChanged = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText)findViewById(R.id.editText);
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int st, int b, int c)
{
onTextChanged = et.getText().toString();
}
@Override
public void beforeTextChanged(CharSequence s, int st, int c, int a)
{
beforeTextChanged = et.getText().toString();
}
@Override
public void afterTextChanged(Editable s)
{
afterTextChanged = et.getText().toString();
Toast.makeText(Activity.this, "before: " + beforeTextChanged
+ '\n' + "on: " + onTextChanged
+ '\n' + "after: " + afterTextChanged
,Toast.LENGTH_SHORT).show();
}
});
}
在这种情况下,假设你从H到喜,输出会改变文:
In this case, let's say you changed the text from "h" to "hi", the output would be:
在H
在喜
后:喜
before: "h"
on: "hi"
after: "hi"
这篇关于TextChangedListener - onTextChanged,beforeTextChanged和afterTextChanged之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!