/*** Set cursor to end of text in edittext when user clicks Next on Keyboard.*/View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {@Overridepublic void onFocusChange(View view, boolean b) {if (b) {((EditText) view).setSelection(((EditText) view).getText().length());}}};
mEditFirstName.setOnFocusChangeListener(onFocusChangeListener);mEditLastName.setOnFocusChangeListener(onFocusChangeListener);
// For a EditText like:<EditTextandroid:id="@+id/EditTextAmount"android:layout_height="wrap_content"android:layout_width="fill_parent"android:hint="@string/amount"android:layout_weight="1"android:text="@string/zero_value"android:inputType="text|numberDecimal"android:maxLength="13"/>
@string="0.00"@string/zero_value="0.00"
// Create a Static boolean flagprivate static boolean returnNext;
// Set caret/cursor to the end on focus changeEditTextAmount.setOnFocusChangeListener(new View.OnFocusChangeListener() {@Overridepublic void onFocusChange(View editText, boolean hasFocus) {if(hasFocus){((EditText) editText).setSelection(((EditText) editText).getText().length());}}});
// Create a touch listener and put caret to the end (no matter where the user touched in the middle of the string)EditTextAmount.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View editText, MotionEvent event) {((EditText) editText).onTouchEvent(event);((EditText) editText).setSelection(((EditText) editText).getText().length());return true;}});
// Implement a Currency Mask with addTextChangedListenerEditTextAmount.addTextChangedListener(new TextWatcher() {@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {String input = s.toString();String output = new String();String buffer = new String();String decimals = new String();String numbers = Integer.toString(Integer.parseInt(input.replaceAll("[^0-9]", "")));
if(returnNext){returnNext = false;return;}
returnNext = true;
if (numbers.equals("0")){output += "0.00";}else if (numbers.length() <= 2){output += "0." + String.format("%02d", Integer.parseInt(numbers));}else if(numbers.length() >= 3){decimals = numbers.substring(numbers.length() - 2);int commaCounter = 0;for(int i=numbers.length()-3; i>=0; i--){if(commaCounter == 3){buffer += ",";commaCounter = 0;}buffer += numbers.charAt(i);commaCounter++;}output = new StringBuilder(buffer).reverse().toString() + "." + decimals;}EditTextAmount.setText(output);EditTextAmount.setSelection(EditTextAmount.getText().length());}
@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {/*String input = s.toString();if(input.equals("0.0")){EditTextAmount.setText("0.00");EditTextAmount.setSelection(EditTextAmount.getText().length());return;}*/}
@Overridepublic void afterTextChanged(Editable s) {
}});
<!-- android:text must be placed before cursorPosition otherwise we'll get IndexOutOfBounds exception--><EditTextandroid:text="@={viewModel.noteText}"cursorPosition="@{viewModel.cursorPosition}" />
open class Event<out T>(private val content: T) {
var hasBeenHandled = falseprivate set // Allow external read but not write
/*** Returns the content and prevents its use again.*/fun getContentIfNotHandled(): T? {return if (hasBeenHandled) {null} else {hasBeenHandled = truecontent}}
/*** Returns the content, even if it's already been handled.*/fun peekContent(): T = content}