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 监听器
基础教程推荐
- NSString intValue 不能用于检索电话号码 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
