android edittext onchange listener(android edittext onchange 监听器)
问题描述
我对 TextWatcher
了解一些,但它会在您输入的每个字符上触发.我想要一个在用户完成编辑时触发的监听器.是否可以?同样在 TextWatcher
中,我得到了一个 Editable
的实例,但我需要一个 EditText
的实例.我怎么得到它?
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?
EDIT:第二个问题更重要.请回答.
EDIT: the second question is more important. Please answer that.
推荐答案
首先,如果 EditText
失去焦点或用户按下完成按钮(这取决于您的实施以及最适合您的方式).其次,只有将 EditText
声明为实例对象,才能在 TextWatcher
中获取 EditText
实例.即使您不应该在 TextWatcher
中编辑 EditText
,因为它不安全.
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
实例放入您的 TextWatcher
实现中,您应该尝试这样的操作:
To be able to get the EditText
instance into your TextWatcher
implementation, you should try something like this:
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.
这篇关于android edittext onchange 监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:android edittext onchange 监听器
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01