2013-07-24 24 views
3

我有以下代碼:的EditText onChangeListener功能的Android

nameOfInf.setOnFocusChangeListener(new OnFocusChangeListener() { 
    if (strTollAmount.length() > 0) { 
     nameOfInf.setBackgroundColor(getResources().getColor(android.R.color.white)); 
     nameOfInf.setTextColor(getResources().getColor(android.R.color.black)); 
    } 
}); 
tollAmount.setOnFocusChangeListener(new OnFocusChangeListener() { 
    if (strInfName.length() > 0) { 
     tollAmount.setBackgroundColor(getResources().getColor(android.R.color.white)); 
     tollAmount.setTextColor(getResources().getColor(android.R.color.black)); 
    } 
}); 

功能檢查,看看是否在文本框中的值以外的任何其他空或空。因此,如果用戶在文本框中輸入內容,背景和前景色應該改變。但這並沒有發生。任何想法如何編輯它?

+2

是它編譯?我認爲'TextBox2>「」''不應該編譯。 – Desert

+0

對不起,我更新了它。 – Si8

回答

8

我想你正在尋找的是TextWatcher(雖然onFocusChanged可能會奏效,但這是另一種方法)。下面是示例代碼:

TextWatcher watcher= new TextWatcher() { 
     public void afterTextChanged(Editable s) { 
      if (TextBox1.getText().toString().equals("")) { 
       TextBox1.setBackgroundColor(getResources().getColor(android.R.color.white)); 
       TextBox1.setTextColor(getResources().getColor(android.R.color.black)); 
      } 

     } 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
       //Do something or nothing.     
     } 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      //Do something or nothing 
     } 
    }; 

    TextBox1.addTextChangedListener(watcher); 

而且你會想用.equals()做字符串比較。

+0

我認爲你有我想要做的事情的想法。我將如何將其納入我的代碼? – Si8

+1

你*應該*能夠複製/粘貼你擁有其他代碼的地方,然後將TextBox1更改爲合適的EditText var。 – TronicZomB

+0

我這樣做的方式是每個EditText都有一個不同的TextWatcher。 – TronicZomB

0

你的方法是我認爲絕對錯誤(編譯?)。你需要測試字段的內容長度。我不確切知道你在代碼中想要做什麼。

這裏有兩種可能的方法:

if (TextBox1.getText().toString().length() > 0) { 
    // is not empty 
} 
else { 
    // is empty 
} 

or 

if (!TextUtils.isEmpty(TextBox1.getText().toString())) { 
    // is not empty 
} 
else { 
    // is empty 
} 
4
txt = (EditText) findViewById(R.id.txtChange); 
    txt.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 
      // TODO Auto-generated method stub 
      if (TextUtils.isEmpty(s.toString().trim())) { 
       txt.setBackgroundColor(Color.RED); 
      } else 
       txt.setBackgroundColor(Color.GREEN); 
     } 

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

     } 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 
    }); 
1

您可以TextWatcher,這裏有一個例子:

final EditText et = new EditText(getApplicationContext()); 

TextWatcher tw = new TextWatcher() { 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
    if (s.length() > 0) { 
     et.setBackgroundColor(Color.WHITE); 
    } else { 
     et.setBackgroundColor(Color.GRAY); 
    } 

    } 

    @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 

    } 
}; 

et.addTextChangedListener(tw); 
setContentView(et);