当我们想要正确猜测EditText的单词时,如何使用Textwatcher来改变EditText数组的背景颜色

时间:2015-09-30 18:24:23

标签: android android-edittext textwatcher

当我们想要正确猜出Textwatcher的单词时,如何使用EditText更改EditText数组的背景颜色?

示例:

public EditText[] edts=new EditText[9];
String[] etd={"f","m","v","f","m","v","f","m","v"};

1 个答案:

答案 0 :(得分:0)

只需在所有EditTexts中添加onTextChangedListener

    public EditText[] edts=new EditText[9];
    String[] etd={"f","m","v","f","m","v","f","m","v"};
    private void addTextChangedListeners() {
        for (int i = 0; i < edts.length; i++) {
            final int idx = i; //necessary for inner class
            edts[i].addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    //No implementation needed.
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (s.toString().equals(etd[idx])) {
                        edts[idx].setBackgroundColor(Color.parseColor("#FF00FF"));
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {
                    //No implementation needed.
                }
            });
        }
    }