2015-01-16 128 views
0

我有8個EditText。設置值時更改EditText的顏色

用戶在某些EditText中輸入一些值,並通過一些計算填充空白的EditText。

我寫的代碼來改變EditText上的顏色如下:

    if (ng1 == 0) { 
           if (Goal >= 0 && Goal <=10) 
            eg1.setText(Float.toString(Goal)); 
           eg1.setTextColor(Color.BLUE); 

          } 

          if (ng2 == 0) { 
           if (Goal >= 0 && Goal <=10) 
            eg2.setText(Float.toString(Goal)); 
           eg2.setTextColor(Color.BLUE); 
          } 

          if (ng3 == 0) { 
           if (Goal >= 0 && Goal <=10) 
            //eg3.setText(""); 
            eg3.setText(Float.toString(Goal)); 
           eg3.setTextColor(Color.BLUE); 
          } 

手段IHE的情況是,用戶可以把2 4,他希望什麼都值3,和EditText上的其餘部分將在一些計算的基礎上填充,並顯示用戶填寫我需要將其更改爲藍色,如果用戶編輯edittext顏色現在是藍色,但我想黑色

最新的方式?

回答

1

嘗試使用onTouchListner,當用戶單擊editText將顏色變爲黑色時。

0

您必須使用文本更改偵聽器。

_edtText.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       // TODO Auto-generated method stub 
       _edtText.setTextColor(Color.BLACK); // you can set color onTextChanged 
      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 
       // TODO Auto-generated method stub 


      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       // TODO Auto-generated method stub 

      } 
     }); 
+0

雖然這是我的工作,但最好提供一個簡要的解釋,說明您在OP代碼中更改了哪些內容以及出了什麼問題。 – shkschneider

+1

在您的代碼中,您不管理其他情況,即Edittext顏色始終設置爲藍色的原因。 –

+0

我想,當用戶在文本框中再次輸入內容時,OP想要將文本的顏色更改爲「黑色」。 –

1

既然你有很多被以同樣的方式處理EditText控制,我建議使用TextWatcherdocumentation)聽文本的變化和避免重複相同的操作對所有3,8,或數不勝數控制。

請注意,下面的例子沒有編譯,而是使用簡單的文本編輯器從內存中編寫,因此可能需要進行更改才能編譯。然而,這只是一個模板,你指出正確的方向

首先,你纔能有相同的TextWatcher處理所有EditText

private class MyTextWatcher implements TextWatcher { 

    private EditText mEditText; 

    public MyTextWatcher(EditText view) { 
     mEditText = view; 
    } 

    public void afterTextChanged (Editable s) {} 

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

    public void onTextChanged (CharSequence s, int start, int before, int count) { 
     //Check if has user focus. if it has, then user is typing smth. set appropriate color 
     mEditText.setTextColor(mEditText.hasFocus() ? Color.BLACK : Color.BLUE); 
    } 
}; 

然後在onCreate()添加textChange監聽器創建一個自定義TextWatcher實施:

eg1 = (EditText)findViewById(R.id.your_eg1_id); 
eg1.addTextChangedListener(new MyTextWatcher(eg1)); 
eg2 = (EditText)findViewById(R.id.your_eg2_id); 
eg2.addTextChangedListener(new MyTextWatcher(eg2)); 
... 
egN = (EditText)findViewById(R.id.your_egN_id); 
egN.addTextChangedListener(new MyTextWatcher(eg3)); 
0

我做什麼,我只是添加ontouch監聽到所有的編輯文本

eg1.setOnTouchListener(new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       eg1.setTextColor(Color.BLACK); 
       return false; 
      } 
     }); 

     eg2.setOnTouchListener(new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       eg2.setTextColor(Color.BLACK); 
       return false; 
      } 
     }); 

     eg3.setOnTouchListener(new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       eg3.setTextColor(Color.BLACK); 
       return false; 
      } 
     }); 

     eg4.setOnTouchListener(new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       eg4.setTextColor(Color.BLACK); 
       return false; 
      } 
     }); 

     eg5.setOnTouchListener(new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       eg5.setTextColor(Color.BLACK); 
       return false; 
      } 
     }); 

     eg6.setOnTouchListener(new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       eg6.setTextColor(Color.BLACK); 
       return false; 
      } 
     }); 

     eg7.setOnTouchListener(new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       eg7.setTextColor(Color.BLACK); 
       return false; 
      } 
     }); 

     eg8.setOnTouchListener(new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       eg8.setTextColor(Color.BLACK); 
       return false; 
      } 
     }); 
+0

誰曾經給我一個下來,你可以告訴我一個更好的方式來做到這一點..... –