Fix inserting dividers in edit text

This commit is contained in:
PavelRatushny 2017-10-30 14:09:00 +02:00
parent d98ff3ce93
commit 8992bd3378

View file

@ -278,7 +278,6 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
if (str.length() > 0) { if (str.length() > 0) {
str = str.substring(0, str.length() - 1); str = str.substring(0, str.length() - 1);
extendedEditText.setText(str); extendedEditText.setText(str);
extendedEditText.setSelection(str.length());
} }
break; break;
default: default:
@ -362,14 +361,12 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
private void registerEditTexts() { private void registerEditTexts() {
TextWatcher textWatcher = new TextWatcher() { TextWatcher textWatcher = new TextWatcher() {
int len = 0; int len = 0;
String strBeforeChanging = "";
@Override @Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
View focusedView = getDialog().getCurrentFocus(); len = charSequence.length();
if (focusedView != null && focusedView instanceof ExtendedEditText) { strBeforeChanging = charSequence.toString();
String str = ((ExtendedEditText) focusedView).getText().toString();
len = str.length();
}
} }
@Override @Override
@ -384,28 +381,37 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
ExtendedEditText focusedEditText = (ExtendedEditText) focusedView; ExtendedEditText focusedEditText = (ExtendedEditText) focusedView;
String str = focusedEditText.getText().toString(); String str = focusedEditText.getText().toString();
int strLength = str.length(); int strLength = str.length();
if (strLength == 2 && len < strLength) { if (len < strLength) {
String strToAppend; String strAfterChanging = str.substring(len);
if (coordinateFormat == PointDescription.FORMAT_DEGREES) { String strDivider = null;
strToAppend = "."; if (strLength == 3) {
} else { if (coordinateFormat == PointDescription.FORMAT_DEGREES) {
strToAppend = ":"; strDivider = ".";
} else {
strDivider = ":";
}
} else if (strLength == 6 && coordinateFormat != PointDescription.FORMAT_DEGREES) {
if (coordinateFormat == PointDescription.FORMAT_MINUTES) {
strDivider = ".";
} else {
strDivider = ":";
}
} else if (strLength == 9 && coordinateFormat == PointDescription.FORMAT_SECONDS) {
strDivider = ".";
} }
focusedEditText.setText(str + strToAppend); if (strDivider != null) {
focusedEditText.setSelection(strLength + 1); String textToSet = strBeforeChanging + strDivider + strAfterChanging;
} else if (strLength == 5 && coordinateFormat != PointDescription.FORMAT_DEGREES && len < strLength) { focusedEditText.setText(textToSet);
String strToAppend; focusedEditText.setSelection(textToSet.length());
if (coordinateFormat == PointDescription.FORMAT_MINUTES) {
strToAppend = ".";
} else {
strToAppend = ":";
} }
focusedEditText.setText(str + strToAppend); } else if (len > strLength) {
focusedEditText.setSelection(strLength + 1); if (strLength > 0 && (".:").contains(str.substring(strLength - 1))) {
} else if (strLength == 8 && coordinateFormat == PointDescription.FORMAT_SECONDS && len < strLength) { focusedEditText.setText(str.substring(0, str.length() - 1));
focusedEditText.setText(str + "."); }
focusedEditText.setSelection(strLength + 1); focusedEditText.setSelection(focusedEditText.getText().length());
} else if ((strLength == DEGREES_MAX_LENGTH && coordinateFormat == PointDescription.FORMAT_DEGREES) }
if ((strLength == DEGREES_MAX_LENGTH && coordinateFormat == PointDescription.FORMAT_DEGREES)
|| (strLength == MINUTES_MAX_LENGTH && coordinateFormat == PointDescription.FORMAT_MINUTES) || (strLength == MINUTES_MAX_LENGTH && coordinateFormat == PointDescription.FORMAT_MINUTES)
|| (strLength == SECONDS_MAX_LENGTH && coordinateFormat == PointDescription.FORMAT_SECONDS)) { || (strLength == SECONDS_MAX_LENGTH && coordinateFormat == PointDescription.FORMAT_SECONDS)) {
if (focusedEditText.getId() == R.id.latitude_edit_text) { if (focusedEditText.getId() == R.id.latitude_edit_text) {