How is it possible to detect if a character is inputted into an EditText which makes it from 0 to 1 characters long and then perform some action?
问题:
回答1:
Since you have a rather abstract question, let me attempt an equally generic answer:
In your onCreate()
, declare and cast your EditText
EditText editText = (EditText) findViewById(R.id.editText);
editText.addTextChangedListener(filterTextWatcher);
And then, outside the onCreate()
, setup the filterTextWatcher
like this:
private TextWatcher filterTextWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// DO THE CALCULATIONS HERE AND SHOW THE RESULT AS PER YOUR CALCULATIONS
int radius = 0;
radius = Integer.valueof(s.toString);
double area = Math.PI * radius * radius;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
EDIT:
Updated code with possible calculation. (UNTESTED CODE: I JUST TYPED IT IN WITHOUT TESTING IT. MODIFY WHERE NECESSARY)
Read more about TextWatcher's here
And here are a few examples to get you started:
- http://www.android-ever.com/2012/06/android-edittext-textwatcher-example.html
- http://www.cybernetikz.com/blog/android-textwatcher-example/
- http://www.allappsdevelopers.com/TopicDetail.aspx?TopicID=22b00052-dad0-4e09-a07e-b74f115ab247
- http://myandroidsolutions.blogspot.in/2012/06/android-edittext-change-listener.html
回答2:
You can use TextWatcher on your EditText and handle whatever you want to do with your user Input data Immediately.
here is an example of formatting immediately when user inputs data
final EditText EditTxtFinancialCode = (EditText) findViewById(R.id.edtNewCpFinancialCode);
EditTxtFinancialCode.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String a = "";
boolean flag = true;
String eachBlock[] = EditTxtFinancialCode.getText().toString().split("-");
for (int i = 0; i < eachBlock.length; i++) {
if (eachBlock[i].length() > 4) {
flag = false;
}
}
if (flag) {
EditTxtFinancialCode.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL)
KeyDel = 1;
return false;
}
});
if (KeyDel == 0) {
if (((EditTxtFinancialCode.getText().length() + 1) % 5) == 0) {
if (EditTxtFinancialCode.getText().toString().split("-").length <= 2) {
EditTxtFinancialCode.setText(EditTxtFinancialCode.getText() + "-");
EditTxtFinancialCode.setSelection(EditTxtFinancialCode.getText().length());
}
}
a = EditTxtFinancialCode.getText().toString();
} else {
a = EditTxtFinancialCode.getText().toString();
KeyDel = 0;
}
} else {
EditTxtFinancialCode.setText(a);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
回答3:
http://developer.android.com/reference/android/widget/TextView.OnEditorActionListener.html
Make a listener.
myTextBox.addTextChangedListener(new TextWatcher() {
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) {
TextView myOutputBox = (TextView) findViewById(R.id.myOutputBox);
myOutputBox.setText(s);
}
});
see http://www.mysamplecode.com/2012/06/android-edittext-text-change-listener.html