Reputation: 3625
I have an EditText and a CheckBox. The EditText has an inputStyle numbers only. When entering a value of 0 in the EditText, I'd like that the CheckBox becomes disabled. But if the entered value is greater than 0, then the CheckBox should be enabled.
The checks should be done on the fly, so while typing and after every character input the check should be done.
I came across afterTextChanged
, but I can't get it to work, can someone help me out here?
public class AddBuilding extends Activity implements TextWatcher {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.building);
myEditText.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
if (Integer.parseInt(myEditText.getText().toString()) < 0)
myCheckBox.setEnabled(false);
else
myCheckBox.setEnabled(true);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
});
Upvotes: 0
Views: 734
Reputation: 5671
Try to use not myEditText.getText().toString()
but s.toString()
Upvotes: 2
Reputation: 5825
If your afterTextChanged method is really inside your onCreate method as shown, then it has the wrong scope, and it will never get called.
Upvotes: 0