TextWatcher的onTextChanged,beforeTextChanged和afterTextChanged之间的差异

Sam*_*age 35 android textwatcher android-textwatcher

在我的Android项目中,我不得不将TextChangedListener(TextWatcher)添加到编辑文本视图中.它有三个部分.

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

这三者的不同之处是什么?我不得不在关键的lisner上实现一个表的搜索,对于我的情况,这三个看起来都是一样的.他们也运作相同.当我输入产品名称的一部分时,表格会重新绘制,只包含那些包含输入文本的产品.但是我使用了这个afterTextChanged()部分.我的代码是

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);
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

那么有人可以给我这三个问题吗?

Sur*_*gch 44

有关参数beforeTextChangedonTextChanged是有点难以在第一理解.看到它们在示例中使用可能会有所帮助.请观看以下演示几次.注意计数.

  • 红色亮点是旧的文本即将由绿色文本替换.
  • 绿色亮点是刚刚更换的红色文字的新文本.

在此输入图像描述

beforeTextChanged

  • start 是红色突出显示文本的开始索引(即将被删除)
  • count红色突出显示文本的长度(即将被删除)
  • after绿色突出显示文本的长度(即将添加)

onTextChanged

  • start是绿色突出显示文本的起始索引(刚刚添加).
    这是一样startbeforeTextChanged.
  • before红色突出显示的文本的长度(刚被删除).
    这是一样countbeforeTextChanged.
  • count绿色突出显示的文本的长度(刚刚添加).
    这是一样afterbeforeTextChanged.

afterTextChanged

  • editable是EditText中的可编辑文本.你可以在这里改变它.这样做会TextWatcher再次触发所有事件.
  • 您没有获得有关更改内容的任何信息.如果您想知道,可以设置跨度onTextChanged,然后在此处查找跨度.

什么时候用哪个?

如果要观察所做的更改,请使用beforeTextChanged()onTextChanged().但是,您不能更改CharSequence这两种方法中的文本.

如果要在更改后进一步修改文本,请执行此操作afterTextChanged().

如果你想自己玩它,这是代码.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    final static int RED_COLOR = Color.parseColor("#fb7373");
    final static int GREEN_COLOR = Color.parseColor("#40de83");

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

        EditText editText = findViewById(R.id.editText);
        final TextView tvBeforeText = findViewById(R.id.tvBeforeText);
        final TextView tvBeforeNumbers = findViewById(R.id.tvBeforeNumbers);
        final TextView tvAfterText = findViewById(R.id.tvAfterText);
        final TextView tvAfterNumbers = findViewById(R.id.tvAfterNumbers);

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                SpannableString spannableString = new SpannableString(s);
                BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(RED_COLOR);
                spannableString.setSpan(backgroundSpan, start, start + count, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvBeforeText.setText(spannableString);
                tvBeforeNumbers.setText("start=" + start + "  count=" + count + " after=" + after);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                SpannableString spannableString = new SpannableString(s);
                BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(GREEN_COLOR);
                spannableString.setSpan(backgroundSpan, start, start + count, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvAfterText.setText(spannableString);
                tvAfterNumbers.setText("start=" + start + " before=" + before + " count=" + count);
            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.i("TAG", "afterTextChanged: " + s);
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="beforeTextChanged" />

    <TextView
        android:id="@+id/tvBeforeText"
        android:textSize="17sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tvBeforeNumbers"
        android:textSize="17sp"
        android:text="start=0 count=0 after=0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_marginTop="20dp"
        android:text="onTextChanged" />

    <TextView
        android:id="@+id/tvAfterText"
        android:textSize="17sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tvAfterNumbers"
        android:textSize="17sp"
        android:text="start=0 count=0 after=0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)


Mic*_*ski 25

onTextChanged 在文本更改期间运行.

afterTextChanged 文本更改后立即运行.

beforeTextChanged 在文本更改之前运行.

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

这是一个例子:

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();
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

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

之前:"h"
开启:"hi"
之后:"hi"

  • 那么, onTextChanged 和 afterTextChanged 之间有什么不同? (7认同)

Jig*_*dya 20

Android TextChangedListener是一种在输入字段的文本更改时调用的触发器.

TextChangedListener 有三个事件.

1.beforeTextChanged:这意味着字符即将被一些新文本替换.该文本是不可编辑的.当您需要查看即将更改的旧文本时,将使用此事件.

2.onTextChanged:已经进行了更改,某些字符已被替换.该文本是不可编辑的.当您需要查看文本中的哪些字符是新的时,可以使用此事件.

3.afterTextChanged:与上面相同,但现在文本是可编辑的.当您需要查看并可能编辑新文本时,将使用此事件.

  • 当您说文本是可编辑/不可编辑时,“文本”是什么意思? (2认同)