TextWatcher的onTextChanged,beforeTextChanged和afterTextChanged之间的区别 [英] Differences between TextWatcher 's onTextChanged, beforeTextChanged and afterTextChanged

查看:1354
本文介绍了TextWatcher的onTextChanged,beforeTextChanged和afterTextChanged之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android项目中,我不得不向编辑文本视图添加 TextChangedListener (TextWatcher)。并包含三个部分:

In my Android project, I have had to add a TextChangedListener (TextWatcher) to an edit text view. And there are three parts to it:


  • onTextChanged()

  • beforeTextChanged()

  • afterTextChanged()

  • onTextChanged()
  • beforeTextChanged()
  • afterTextChanged()

这三个有什么区别?我不得不在键侦听器上实现对表的搜索,对于我来说,这三个表看起来都一样。它们的功能也相同。当我输入一部分产品名称时,该表将仅使用其中包含输入文字的那些产品进行重绘。但是我使用了 afterTextChanged()部分。我的代码是:

What are the differences of these three? I have had to implement a search of a table on the key listener and for my case all these three looked the same. Also they functioned the same. When I input a part of a product name, the table redraws with only those products that contain entered text in it. But I used the afterTextChanged() part. My code is:

EditProduct.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub

            // System.out.println("onTextChanged"+s);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
            // System.out.println("beforeTextChanged"+s);
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            // System.out.println("afterTextChanged"+s);

            String new_prx = s.toString();

            System.out.println(s);
            mini_productList = new ArrayList<Product>();

            // mini_productList
            int count = 0;
            if (new_prx.equals("")) {

                loadtableProducts(productList);

            } else {

                for (int i = 0; i < productList.size(); i++) {

                    if (productList.get(i).getDescription().toString()
                            .substring(0, (new_prx.length()))
                            .equalsIgnoreCase(new_prx)) {
                        mini_productList.add(productList.get(i));
                        count++;

                    }
                }

                loadtableProducts(mini_productList);
            }
        }
    });

那么有人可以给我解释这三个方面吗?

So can someone give me an explanation on these three?

推荐答案

onTextChanged 在更改文本期间运行。

onTextChanged runs during the text changing.

afterTextChanged 在更改文本后立即运行。

afterTextChanged runs immediately after the text is changed.

beforeTextChanged 在更改文本之前立即运行。

beforeTextChanged runs the instant before the text is changed.

根据要分配变量或执行操作的时间,您可能希望在更改之前或之后运行代码。

Depending on when you want to assign variables or do things, you may want to run the code the instant before the change, or the instant after.

下面是一个示例:

String afterTextChanged = "";
String beforeTextChanged = "";
String onTextChanged = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    et = (EditText)findViewById(R.id.editText);

    et.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int st, int b, int c) 
        {
            onTextChanged = et.getText().toString();
        }

        @Override
        public void beforeTextChanged(CharSequence s, int st, int c, int a) 
        {
            beforeTextChanged = et.getText().toString();
        }

        @Override
        public void afterTextChanged(Editable s) 
        {
            afterTextChanged = et.getText().toString();
            Toast.makeText(Activity.this, "before: " + beforeTextChanged
                                           + '\n' + "on: " + onTextChanged 
                                           + '\n' + "after: " + afterTextChanged
                           ,Toast.LENGTH_SHORT).show();
        }
    });
}

在这种情况下,假设您将文本从 h更改为 hi,输出为:

In this case, let's say you changed the text from "h" to "hi", the output would be:


之前: h

on: hi

之后:嗨

before: "h"
on: "hi"
after: "hi"

这篇关于TextWatcher的onTextChanged,beforeTextChanged和afterTextChanged之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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