changing focus on multiple edittext boxes
I've written some code to test if a user has changed the values in any of 6 ed开发者_StackOverflow社区ittext boxes, but the code looks bulky and redundant. I'm wondering if there is a more elegant way to do this.
The user inputs numbers into all of 6 edittext boxes, and after each entry, a calculation routine checks to be sure all boxes have valid numbers before doing the calculation. The kistener has to fire on a lost foucus so that the number is in the box before the calculations are done. and outputing a result. If the user want to go back and change a value in any one of the 6 edittext boxes, the calulation routine runs again.been I've set up 6 listeners in the onCreate section for each edittext box. Then I have 6 separate routines to test for each hasFocus==false.
Within the onCreate section i set a listener for each of the 6 user input edottext boxes:
et1.setOnFocusChangeListener(this);
.
.
.
.
et6.setOnFocusChangeListener(this);
Then I have a method to check if any edittext box has lost focus, suggesting theat the user changed some data, and therfore re-run the calulations.
I do a check for each of the 6 user input edittext boxes as follows:@Override
public void onFocusChange(View v, boolean hasFocus)
{ if ((v == findViewById(R.id.et1)) && (hasFocus==false))
{do calculations }
.
.
.
.
if ((v == findViewById(R.id.et6)) && (hasFocus==false))
{do calculations }
}
Forgive me if I've made any typos in the code above. It runs on my platform, but I would like to learn a better way to do this.
I would think checking which view has lost focus is unnecessary, unless the calculations are different depending on which box has lost its focus. If the calculations are the same, just try:
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
//do calculations
}
}
If you're only caring that one of the six has lost focus, then any time the focus changes, just check to see if it's not focused, then perform the calculations.
Also, just for reference, it's more efficient to check if(v.getId() == R.id.et1)
rather than calling findViewById()
unnecessarily.
EDIT: Just thinking, haven't tested this, but something like this also might be more effective, to cause the calculations to occur only if the value was actually changed:
private String last;
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
last = ((EditText)v).getText().toString();
} else {
if(!((EditText)v).getText().toString().equals(last)) {
//do calculations
}
}
}
You would probably only want to make changes if the text actually changed. So you should add a TextChangedListener for your edittexts. So for each of your edit text's:
et1.addTextChangedListener(this);
...
et6.addTextChangedListener(this);
Then, let your class implement TextWatcher
@Override
public void afterTextChanged(Editable s) {
// do calculations
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
精彩评论