android edittext onchange 监听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11134144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android edittext onchange listener
提问by prongs
I know a little bit about TextWatcher
but that fires on every character you enter. I want a listener that fires whenever the user finishes editing. Is it possible? Also in TextWatcher
I get an instance of Editable
but I need an instance of EditText
. How do I get that?
我知道一点,TextWatcher
但这会在您输入的每个字符上触发。我想要一个在用户完成编辑时触发的侦听器。是否可以?同样在TextWatcher
我得到一个实例Editable
但我需要一个EditText
. 我怎么得到它?
EDIT: the second question is more important. Please answer that.
编辑:第二个问题更重要。请回答那个。
回答by Cata
First, you can see if the user finished editing the text if the EditText
loses focus or if the user presses the done button (this depends on your implementation and on what fits the best for you).
Second, you can't get an EditText
instance within the TextWatcher
only if you have declared the EditText
as an instance object. Even though you shouldn't edit the EditText
within the TextWatcher
because it is not safe.
首先,如果EditText
失去焦点或用户按下完成按钮(这取决于您的实现以及最适合您的方式),您可以查看用户是否完成了文本的编辑。其次,你不能让一个EditText
在中实例TextWatcher
只有当你已经宣布EditText
为一个实例对象。即使您不应该EditText
在其中编辑,TextWatcher
因为它不安全。
EDIT:
编辑:
To be able to get the EditText
instance into your TextWatcher
implementation, you should try something like this:
为了能够将EditText
实例加入到您的TextWatcher
实现中,您应该尝试这样的操作:
public class YourClass extends Activity {
private EditText yourEditText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
yourEditText = (EditText) findViewById(R.id.yourEditTextId);
yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// you can call or do what you want with your EditText here
// yourEditText...
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
}
Note that the above sample might have some errors but I just wanted to show you an example.
请注意,上面的示例可能有一些错误,但我只是想向您展示一个示例。
回答by RNGuy
It was bothering me that implementing a listener for all of my EditText fields required me to have ugly, verbose code so I wrote the below class. May be useful to anyone stumbling upon this.
让我烦恼的是,为我的所有 EditText 字段实现一个监听器需要我有丑陋、冗长的代码,所以我写了下面的类。可能对任何遇到此问题的人有用。
public abstract class TextChangedListener<T> implements TextWatcher {
private T target;
public TextChangedListener(T target) {
this.target = target;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
this.onTextChanged(target, s);
}
public abstract void onTextChanged(T target, Editable s);
}
Now implementing a listener is a little bit cleaner.
现在实现一个监听器更简洁一些。
editText.addTextChangedListener(new TextChangedListener<EditText>(editText) {
@Override
public void onTextChanged(EditText target, Editable s) {
//Do stuff
}
});
As for how often it fires, one could maybe implement a check to run their desired code in //Do stuff
after a given a
至于它多久触发一次,可以实现一个检查以在//Do stuff
给定的时间后运行他们想要的代码
回答by Prakash
Anyone using ButterKnife. You can use like:
任何使用ButterKnife 的人。你可以使用像:
@OnTextChanged(R.id.zip_code)
void onZipCodeTextChanged(CharSequence zipCode, int start, int count, int after) {
}
回答by Jawad Zeb
I have done it using AutotextView
:
我已经使用AutotextView
:
AutotextView textView = (AutotextView) findViewById(R.id.autotextview);
textView.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
seq = cs;
}
@Override
public void beforeTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
new SearchTask().execute(seq.toString().trim());
}
});
回答by Ramesh sambu
myTextBox.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) {
TextView myOutputBox = (TextView) findViewById(R.id.myOutputBox);
myOutputBox.setText(s);
}
});
回答by user2566490
TextWatcher
didn't work for me as it kept firing for every EditText
and messing up each others values.
TextWatcher
对我不起作用,因为它一直在为EditText
每个人开火并弄乱彼此的价值观。
Here is my solution:
这是我的解决方案:
public class ConsultantTSView extends Activity {
.....
//Submit is called when I push submit button.
//I wanted to retrieve all EditText(tsHours) values in my HoursList
public void submit(View view){
ListView TSDateListView = (ListView) findViewById(R.id.hoursList);
String value = ((EditText) TSDateListView.getChildAt(0).findViewById(R.id.tsHours)).getText().toString();
}
}
Hence by using the getChildAt(xx)
method you can retrieve any item in the ListView
and get the individual item using findViewById
. And it will then give the most recent value.
因此,通过使用该getChildAt(xx)
方法,您可以检索 中的任何项目ListView
并使用 获取单个项目findViewById
。然后它会给出最新的值。
回答by Nuno Gon?alves
As far as I can think bout it, there's only two ways you can do it. How can you know the user has finished writing a word? Either on focus lost, or clicking on an "ok" button. There's no way on my mind you can know the user pressed the last character...
据我所知,只有两种方法可以做到。你怎么知道用户写完一个词?要么失去焦点,要么点击“确定”按钮。我想你不可能知道用户按下了最后一个字符......
So call onFocusChange(View v, boolean hasFocus)
or add a button and a click listener to it.
所以调用onFocusChange(View v, boolean hasFocus)
或添加一个按钮和一个点击监听器。
回答by phnghue
The Watcher method fires on every character input. So, I built this code based on onFocusChange method:
Watcher 方法在每个字符输入时触发。所以,我基于 onFocusChange 方法构建了这段代码:
public static boolean comS(String s1,String s2){
if (s1.length()==s2.length()){
int l=s1.length();
for (int i=0;i<l;i++){
if (s1.charAt(i)!=s2.charAt(i))return false;
}
return true;
}
return false;
}
public void onChange(final EditText EdTe, final Runnable FRun){
class finalS{String s="";}
final finalS dat=new finalS();
EdTe.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {dat.s=""+EdTe.getText();}
else if (!comS(dat.s,""+EdTe.getText())){(new Handler()).post(FRun);}
}
});
}
To using it, just call like this:
要使用它,只需像这样调用:
onChange(YourEditText, new Runnable(){public void run(){
// V V YOUR WORK HERE
}}
);
You can ignore the comS function by replace the !comS(dat.s,""+EdTe.getText()) with !equal function. However the equal function itself some time work not correctly in run time.
您可以通过将 !comS(dat.s,""+EdTe.getText()) 替换为 !equal 函数来忽略 comS 函数。然而,equal 函数本身有时在运行时不能正常工作。
The onChange listener will remember old data of EditText when user focus typing, and then compare the new data when user lose focus or jump to other input. If comparing old String not same new String, it fires the work.
onChange 监听器会在用户焦点输入时记住 EditText 的旧数据,然后在用户失去焦点或跳转到其他输入时比较新数据。如果比较旧字符串与新字符串不同,则会触发工作。
If you only have 1 EditText, then u will need to make a ClearFocus function by making an Ultimate Secret Transparent Micro EditText outside the windows and request focus to it, then hide the keyboard via Import Method Manager.
如果您只有 1 个 EditText,那么您需要通过在窗口外制作 Ultimate Secret Transparent Micro EditText 并请求焦点来制作 ClearFocus 功能,然后通过导入方法管理器隐藏键盘。