单个 TextWatcher 用于多个 EditText?

发布于 2024-10-19 09:40:10 字数 121 浏览 1 评论 0原文

我的活动中有多个 (12) 个 EditText,每对 2 个。当其中的文本发生更改时,我想对每对执行一个操作。我是否需要有 6 个不同的 TextWatcher,或者有没有办法使用同一个 TextWatcher 进行某种切换?

I have multiple(12) EditTexts in my activity in pairs of 2. I want to do an action for each pair when the text in them changes. Do I need to have 6 different TextWatchers or is there a way to use the same one for or and do some kind of switch?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风情万种。 2024-10-26 09:40:10

您可以将相同的 TextWatcher 监视附加到每个 EditText。根据您需要执行的操作,您可能需要创建 TextWatcher 有一些背景。

You can attach the same TextWatcher watch to each EditText. Depending on what you need to do you may need to create you implementation of TextWatcher with some context.

傲性难收 2024-10-26 09:40:10

我需要这个,所以我做了一个可重复使用的......我扩展了 textwatcher 类,这样我就可以传递我想观看的视图。

/**
 *  
 * A TextWatcher which can be reused
 *  
 */
public class ReusableTextWatcher implements TextWatcher {

    private TextView view;

    // view represents the view you want to watch. Should inherit from
    // TextView
    private GenericTextWatcher(View view) {

        if (view instanceof TextView)
            this.view = (TextView) view;
        else
            throw new ClassCastException(
                    "view must be an instance Of TextView");
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i,
            int before, int after) {
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int before,
            int count) {

        int id = view.getId();

        if (id == R.id.someview){
            //do the stuff you need to do for this particular view

            }



        if (id == R.id.someotherview){
            //do the stuff you need to do for this other particular view

            }

    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
}

然后要使用它,我会执行类似的操作来注册它:

myEditText.addTextChangedListener(new ReusableTextWatcher(myEditText));

I needed this so i made a reusable one ...i extended off textwatcher class so i could pass in the view i want to watch.

/**
 *  
 * A TextWatcher which can be reused
 *  
 */
public class ReusableTextWatcher implements TextWatcher {

    private TextView view;

    // view represents the view you want to watch. Should inherit from
    // TextView
    private GenericTextWatcher(View view) {

        if (view instanceof TextView)
            this.view = (TextView) view;
        else
            throw new ClassCastException(
                    "view must be an instance Of TextView");
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i,
            int before, int after) {
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int before,
            int count) {

        int id = view.getId();

        if (id == R.id.someview){
            //do the stuff you need to do for this particular view

            }



        if (id == R.id.someotherview){
            //do the stuff you need to do for this other particular view

            }

    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
}

then to use it i do something like this to register it:

myEditText.addTextChangedListener(new ReusableTextWatcher(myEditText));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文