1

アクティビティに2つのペアで複数の(12)EditTextがあります。ペアのテキストが変更されたときに、ペアごとにアクションを実行したいと思います。6つの異なるTextWatcherが必要ですか、それとも同じものをまたはに使用して何らかの切り替えを行う方法はありますか?

4

2 に答える 2

2

同じTextWatcherウォッチを各EditTextにアタッチできます。何をする必要があるかに応じて、何らかのコンテキストでTextWatcherの実装を作成する必要がある場合があります。

于 2011-02-27T01:14:45.680 に答える
1

これが必要だったので、再利用可能なものを作成しました...私は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));
于 2014-05-08T00:19:02.703 に答える