安卓平变化的EditText监听器监听器、卓平、EditText

2023-09-14 23:01:34 作者:笑谋

我知道 TextWatcher 一点点,但上每一个字符触发输入。我希望有一个监听器,只要用户完成编辑触发。可能吗?此外,在 TextWatcher 我得到编辑的实例,但我需要的实例的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: the second question is more important. Please answer that.

推荐答案

首先,你可以看到,如果用户完成了编辑的文本,如果的EditText 失去焦点,或者用户presses完成按钮(这取决于具体的实现和最适合自己的最适合你)。 其次,你不仅可以,如果你已经宣布textwatcher内获得的EditText 实例的EditText 作为一个实例对象。即使你不应该编辑的EditText textwatcher ,因为它是不安全的。

first, you can see if the user finished to edit the text if the EditText lose focus or if the user presses 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 a 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.

 
精彩推荐
图片推荐