TextWatcher
You can do the addTextChangedListener() method one time.
First step
1 | implements TextWatcher |
Then you have a error
add unimplemented method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @Override public void afterTextChanged(Editable s) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } |
Now you can need declare you editText and to add listener in onCreate()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | public class MainActivity extends ActionBarActivity implements OnClickListener,TextWatcher { EditText mEditText; Button mButtonSave; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButtonSave = (Button) findViewById(R.id.activity_main_button_save); mEditText = (EditText) findViewById(R.id.activity_main_editText1); mEditText.setOnClickListener( this ); mButtonSave.setEnabled( false ); mEditText.addTextChangedListener( this ); } @Override public void afterTextChanged(Editable s) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (mEditText.getText().length() == 0 ) { mButtonSave.setEnabled( false ); } else { mButtonSave.setEnabled( true ); } } } |