Android TextWatcher根据文本更改EditText文本颜色。多个EditText

时间:2015-10-28 14:39:59

标签: android android-textwatcher

我正在尝试实现 TextWatcher ,以根据每个中的文本更改Relative布局中每个EditText的文本颜色。我也有一个按钮,当我单击按钮时,Listener首次使用TextWatcher随机填充EditText(“IRE”,“SCO”等)。

代码:

public class MainActivity extends Activity implements TextWatcher{
private EditText team1,team2,team3,team4,team5,team6,team7,team8, team9,team10,team11,team12,team13,team14,team15;

private EditText text;

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

    button = (Button) findViewById(R.id.luckdipbutton);
    holeScores = new ArrayList<EditText>();
    rl = (ScrollView) findViewById(R.id.scrollView);
    rl.setBackgroundColor(Color.BLACK);

    team1 = (EditText) findViewById(R.id.team1);
    // team1.addTextChangedListener(this);
    team2 = (EditText) findViewById(R.id.team2);
    team3 = (EditText) findViewById(R.id.team3);
    team4 = (EditText) findViewById(R.id.team4);
    team5 = (EditText) findViewById(R.id.team5);
    team6 = (EditText) findViewById(R.id.team6);
    team7 = (EditText) findViewById(R.id.team7);
    team8 = (EditText) findViewById(R.id.team8);
    team9 = (EditText) findViewById(R.id.team9);
    team10 = (EditText) findViewById(R.id.team10);
    team11 = (EditText) findViewById(R.id.team11);
    team12 = (EditText) findViewById(R.id.team12);
    team13 = (EditText) findViewById(R.id.team13);
    team14 = (EditText) findViewById(R.id.team14);
    team15 = (EditText) findViewById(R.id.teamwinner);


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {


        if (s.toString().equals("IRE"))

        {

            this.text.setTextColor(Color.GREEN);
        }

        else if (s.toString().equals("SCO"))
        {
            this.text.setTextColor(Color.WHITE);
        }
        else if (s.toString().equals("AUS"))
        {
            this.text.setTextColor(Color.YELLOW);
        }
        else if (s.toString().equals("NZL"))
        {
            this.text.setTextColor(Color.RED);
        }

        else {

        }

    }

1 个答案:

答案 0 :(得分:0)

试试这个:

    private EditText team1, team2, team3, team4, team5, team6, team7,team8, team9, team10, team11, team12, team13, team14, team15;
    private ScrollView rl;
    private Button button;
    private ArrayList<EditText> allEditText;


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

    button = (Button) findViewById(R.id.luckdipbutton);
    rl = (ScrollView) findViewById(R.id.scrollView);
    rl.setBackgroundColor(Color.BLACK);
    allEditText = new ArrayList<EditText>();


    team1 = (EditText) findViewById(R.id.team1);
    team2 = (EditText) findViewById(R.id.team2);
    team3 = (EditText) findViewById(R.id.team3);
    team4 = (EditText) findViewById(R.id.team4);
    team5 = (EditText) findViewById(R.id.team5);
    team6 = (EditText) findViewById(R.id.team6);
    team7 = (EditText) findViewById(R.id.team7);
    team8 = (EditText) findViewById(R.id.team8);
    team9 = (EditText) findViewById(R.id.team9);
    team10 = (EditText) findViewById(R.id.team10);
    team11 = (EditText) findViewById(R.id.team11);
    team12 = (EditText) findViewById(R.id.team12);
    team13 = (EditText) findViewById(R.id.team13);
    team14 = (EditText) findViewById(R.id.team14);
    team15 = (EditText) findViewById(R.id.teamwinner);

    allEditText.add(team1);
    allEditText.add(team2);
    allEditText.add(team3);
    allEditText.add(team4);
    allEditText.add(team5);
    allEditText.add(team6);
    allEditText.add(team7);
    allEditText.add(team8);
    allEditText.add(team9);
    allEditText.add(team10);
    allEditText.add(team11);
    allEditText.add(team12);
    allEditText.add(team13);
    allEditText.add(team14);
    allEditText.add(team15);


    addTextWatcherToAllEditText();




    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        }

public class GenericTextWatcher implements TextWatcher

{
    private EditText text;

    public GenericTextWatcher(EditText text)
    {
        this.text = text;
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {


            if (s.toString().equals("IRE"))

            {
                this.text.setTextColor(Color.parseColor("#FF228822"));

            } else if (s.toString().equals("SCO")) {

                this.text.setTextColor(Color.parseColor("#FFFFFFFF"));

            } else if (s.toString().equals("AUS")) {

                this.text.setTextColor(Color.parseColor("#FFDAA520"));

            } else if (s.toString().equals("NZL")) {

                this.text.setTextColor(Color.parseColor("#FF000000"));

            } else if (s.toString().equals("WAL")){

                this.text.setTextColor(Color.parseColor("#FFFF0000"));

            }
              else if (s.toString().equals("RSA")){

                this.text.setTextColor(Color.parseColor("#FF808000"));
            }
            else if (s.toString().equals("ARG")){

                this.text.setTextColor(Color.parseColor("#FF87CEFA"));
            }
            else if (s.toString().equals("FRA")){

                this.text.setTextColor(Color.parseColor("#FF4169E1"));
            }
            else{

                this.text.setTextColor(Color.parseColor("#FFFFFFFF"));

            }


    }
}


    private void addTextWatcherToAllEditText()
    {
    for(EditText currEditText : allEditText)
    {
        currEditText.addTextChangedListener(new   GenericTextWatcher(currEditText));
    }
}

}