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 监听器
基础教程推荐
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- 如何将多个组件添加到 PickerView? 2022-01-01
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
- Android:STATE_SELECTED不工作 2022-01-01
- 我的 UIImageView 的任务 2022-01-01
- 在 Android 模拟器中激活网络位置提供程序? 2022-01-01
- 突出显示朗读文本(在 iPhone 的故事书类型应用程序中) 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
