Add auto switching to the next edit text

This commit is contained in:
Alexander Sytnyk 2018-02-16 16:45:36 +02:00
parent 69c0a12a6e
commit 900796df8e
4 changed files with 82 additions and 50 deletions

View file

@ -80,17 +80,17 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
<net.osmand.plus.widgets.EditTextEx
android:id="@+id/lat_first_input_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="xx"
android:imeOptions="actionNext"
android:inputType="number"
android:maxLength="2"
android:maxLines="1"
android:theme="@style/CoordinateInputEditText"/>
android:theme="@style/CoordinateInputEditText"
tools:hint="xx"
tools:maxLength="2"/>
<TextView
android:id="@+id/lat_first_separator_tv"
@ -101,7 +101,7 @@
android:textSize="@dimen/dialog_header_text_size"
tools:text="°"/>
<EditText
<net.osmand.plus.widgets.EditTextEx
android:id="@+id/lat_second_input_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
@ -122,7 +122,7 @@
android:textSize="@dimen/dialog_header_text_size"
tools:text="."/>
<EditText
<net.osmand.plus.widgets.EditTextEx
android:id="@+id/lat_third_input_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
@ -224,17 +224,17 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
<net.osmand.plus.widgets.EditTextEx
android:id="@+id/lon_first_input_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="yy"
android:imeOptions="actionNext"
android:inputType="number"
android:maxLength="2"
android:maxLines="1"
android:theme="@style/CoordinateInputEditText"/>
android:theme="@style/CoordinateInputEditText"
tools:hint="yy"
tools:maxLength="2"/>
<TextView
android:id="@+id/lon_first_separator_tv"
@ -245,7 +245,7 @@
android:textSize="@dimen/dialog_header_text_size"
tools:text="°"/>
<EditText
<net.osmand.plus.widgets.EditTextEx
android:id="@+id/lon_second_input_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
@ -266,7 +266,7 @@
android:textSize="@dimen/dialog_header_text_size"
tools:text="."/>
<EditText
<net.osmand.plus.widgets.EditTextEx
android:id="@+id/lon_third_input_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
@ -322,7 +322,7 @@
android:layout_height="@dimen/bottom_sheet_list_item_height"
tools:background="@color/route_info_bottom_view_bg_dark">
<EditText
<net.osmand.plus.widgets.EditTextEx
android:id="@+id/point_name_et"
android:layout_width="0dp"
android:layout_height="match_parent"

View file

@ -23,7 +23,6 @@ import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.TypedValue;
@ -63,6 +62,7 @@ import net.osmand.plus.base.MapViewTrackingUtilities;
import net.osmand.plus.dashboard.DashLocationFragment;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.mapmarkers.adapters.CoordinateInputAdapter;
import net.osmand.plus.widgets.EditTextEx;
import net.osmand.util.LocationParser;
import net.osmand.util.MapUtils;
@ -93,7 +93,7 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
private OnMapMarkersSavedListener listener;
private View mainView;
private List<EditText> editTexts = new ArrayList<>();
private List<EditTextEx> editTexts = new ArrayList<>();
private CoordinateInputAdapter adapter;
private boolean lightTheme;
@ -415,8 +415,8 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
editTexts.clear();
for (int id : ids) {
View v = mainView.findViewById(id);
if (v != null && v instanceof EditText && v.getVisibility() == View.VISIBLE) {
editTexts.add((EditText) mainView.findViewById(id));
if (v != null && v instanceof EditTextEx && v.getVisibility() == View.VISIBLE) {
editTexts.add((EditTextEx) mainView.findViewById(id));
}
}
}
@ -439,19 +439,12 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
@Override
public void afterTextChanged(Editable editable) {
View focusedView = getDialog().getCurrentFocus();
if (focusedView != null && focusedView instanceof EditText) {
EditText focusedEditText = (EditText) focusedView;
String str = focusedEditText.getText().toString();
int currentLength = str.length(); // todo
// if (currentLength > strLength) {
// int pointIndex = str.indexOf(".");
// if (pointIndex != -1) {
// int currentAccuracy = str.substring(pointIndex + 1).length();
// if (currentAccuracy >= accuracy) {
// switchEditText(focusedEditText.getId(), true);
// }
// }
// }
if (focusedView != null && focusedView instanceof EditTextEx) {
EditTextEx et = (EditTextEx) focusedView;
int currentLength = et.getText().length();
if (et.getMaxSymbolsCount() > 0 && currentLength > strLength && currentLength >= et.getMaxSymbolsCount()) {
switchEditText(et.getId(), true);
}
}
}
};
@ -568,20 +561,17 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
int format = getMyApplication().getSettings().COORDS_INPUT_FORMAT.get();
String firstSeparator = CoordinateInputFormats.getFirstSeparator(format);
int secondPartSymbols = CoordinateInputFormats.getSecondPartSymbolsCount(format);
int firstPartSymbols = CoordinateInputFormats.getFirstPartSymbolsCount(format);
setupEditTextEx(R.id.lat_first_input_et, firstPartSymbols, true);
setupEditTextEx(R.id.lon_first_input_et, firstPartSymbols, false);
String firstSeparator = CoordinateInputFormats.getFirstSeparator(format);
((TextView) mainView.findViewById(R.id.lat_first_separator_tv)).setText(firstSeparator);
((TextView) mainView.findViewById(R.id.lon_first_separator_tv)).setText(firstSeparator);
InputFilter[] secondInputFilters = {new InputFilter.LengthFilter(secondPartSymbols)};
EditText latSecondEt = (EditText) mainView.findViewById(R.id.lat_second_input_et);
EditText lonSecondEt = (EditText) mainView.findViewById(R.id.lon_second_input_et);
latSecondEt.setFilters(secondInputFilters);
lonSecondEt.setFilters(secondInputFilters);
latSecondEt.setHint(createHint(secondPartSymbols, 'x'));
lonSecondEt.setHint(createHint(secondPartSymbols, 'y'));
int secondPartSymbols = CoordinateInputFormats.getSecondPartSymbolsCount(format);
setupEditTextEx(R.id.lat_second_input_et, secondPartSymbols, true);
setupEditTextEx(R.id.lon_second_input_et, secondPartSymbols, false);
boolean containsThirdPart = CoordinateInputFormats.containsThirdPart(format);
@ -593,19 +583,12 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
if (containsThirdPart) {
String secondSeparator = CoordinateInputFormats.getSecondSeparator(format);
int thirdPartSymbols = CoordinateInputFormats.getThirdPartSymbolsCount(format);
((TextView) mainView.findViewById(R.id.lat_second_separator_tv)).setText(secondSeparator);
((TextView) mainView.findViewById(R.id.lon_second_separator_tv)).setText(secondSeparator);
InputFilter[] thirdInputFilters = {new InputFilter.LengthFilter(thirdPartSymbols)};
EditText latThirdEt = (EditText) mainView.findViewById(R.id.lat_third_input_et);
EditText lonThirdEt = (EditText) mainView.findViewById(R.id.lon_third_input_et);
latThirdEt.setFilters(thirdInputFilters);
lonThirdEt.setFilters(thirdInputFilters);
latThirdEt.setHint(createHint(thirdPartSymbols, 'x'));
lonThirdEt.setHint(createHint(thirdPartSymbols, 'y'));
int thirdPartSymbols = CoordinateInputFormats.getThirdPartSymbolsCount(format);
setupEditTextEx(R.id.lat_third_input_et, thirdPartSymbols, true);
setupEditTextEx(R.id.lon_third_input_et, thirdPartSymbols, false);
}
addEditTexts(R.id.lat_first_input_et, R.id.lat_second_input_et, R.id.lat_third_input_et,
@ -625,6 +608,12 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
editTexts.get(0).requestFocus();
}
private void setupEditTextEx(@IdRes int etId, int symbols, boolean lat) {
EditTextEx et = (EditTextEx) mainView.findViewById(etId);
et.setMaxSymbolsCount(symbols);
et.setHint(createHint(symbols, lat ? 'x' : 'y'));
}
private String createHint(int symbolsCount, char symbol) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < symbolsCount; i++) {

View file

@ -43,6 +43,10 @@ public class CoordinateInputFormats {
return format == DD_MM_MMM || format == DD_MM_MMMM || format == DD_MM_SS;
}
public static int getFirstPartSymbolsCount(@CoordinateInputFormatDef int format) {
return 2;
}
public static int getSecondPartSymbolsCount(@CoordinateInputFormatDef int format) {
switch (format) {
case DD_MM_MMM:

View file

@ -0,0 +1,39 @@
package net.osmand.plus.widgets;
import android.content.Context;
import android.support.v7.widget.AppCompatEditText;
import android.text.InputFilter.LengthFilter;
import android.util.AttributeSet;
public class EditTextEx extends AppCompatEditText {
private int maxSymbolsCount = -1;
public int getMaxSymbolsCount() {
return maxSymbolsCount;
}
public void setMaxSymbolsCount(int maxSymbolsCount) {
this.maxSymbolsCount = maxSymbolsCount;
setMaxSymbols();
}
public EditTextEx(Context context) {
super(context);
}
public EditTextEx(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EditTextEx(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void setMaxSymbols() {
if (maxSymbolsCount > 0) {
setFilters(new LengthFilter[]{new LengthFilter(maxSymbolsCount)});
}
}
}