android edittext onchange 监听器 [英] android edittext onchange listener

查看:38
本文介绍了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 监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆