what I am looking to do is when a certain phrase is typed, it runs a method after that phrase is typed. how would I do this?
First you need the import statements as this :
[HIGH]import android.text.TextWatcher;
import android.text.Editable;
[/HIGH]
then the way I have done it (this makes it work for different edit text objects) is to implement the textWatcher to your activity :
[HIGH]public class youractivity extends Activity implements TextWatcher [/HIGH]
This will then create an option to create unimplemented method (your activity name will have that wiggly line under it) and these are :
[HIGH]@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@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
}[/HIGH]
I suspect from what you are wanting to do , I would use the :
[HIGH]@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}[/HIGH]
as a test lets say your editText is calls enterhere and you want to show a message when hello is entered , the following should do the trick :
in the onCreate method you would first to this :
[HIGH] enterhere.addTextChangedListener(this);[/HIGH]
Then in the afterTextChanged method try this :
[HIGH] @Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if (s == enterehere.getEditableText()) {
if (enterhere.getText().length() > 0 ) {
if (enterhere.getText().toString()=="hello") {
// Put your message here such as a Toast message
}
}
}
}[/HIGH]
Thanks
TimCS