2015-10-28 194 views
1

我試圖實現TextWatcher根據每個文本中的文本更改相對佈局中的每個EditText的文本顏色。我也有一個按鈕,當我點擊按鈕時,監聽器首次使用TextWatcher隨機填充EditText(「IRE」,「SCO」...等)。Android TextWatcher根據文本更改EditText文本顏色。多個EditText

代碼:

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 { 

     } 

    } 
+0

我不明白。你的問題是什麼? – nbaroz

+0

「team1」的textwatcher也是如此? – JAPK

+0

嘗試將TextWatcher添加到每個視圖。 –

回答

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)); 
    } 
} 

}