ubu
02/25/2020, 2:41 PMEditText , InputFilter and new-line character insertion.
Given this method signature from official docs:
public abstract CharSequence filter (CharSequence source,
int start,
int end,
Spanned dest,
int dstart,
int dend)
and these attributes set on `EditText`:
android:inputType="textMultiLine|textNoSuggestions"
android:textIsSelectable="true"
Suppose we have a text FooBar inside our EditText (set with the attributes mentioned above). When placing cursor between these two words (Foo|Bar) and pressing Enter, we get:
source = "\n",
start = 0,
end = 0
dstart = 3,
dend = 3,
dest = "FooBar"
As you see, new-line character "\n" is inserted at the cursor position. This is somewhat expected and this is good. I just need to detect this insertion and but ignore it by returning "" instead of "\n".
Let’s start again. Suppose we have a text FooBar inside our EditText. Let’s place our cursor between these two words (Foo|Bar) and type 123. Now you should have Foo123|Bar. Then press Enter.
This is what I expect:
source = "\n",
start = 0,
end = 0
dstart = 6,
dend = 6,
dest = "Foo123Bar"
As before, I would then return “” instead of “\n”. But stop. This is what I actually got:
source = "",
start = 0,
end = 0
dstart = 6,
dend = 9,
dest = "Foo123Bar"
Why do InputFilter behaviour differ? I might misunderstand something, but this seems to be an inconsistent behaviour. Any idea how to fix it? Or maybe there is an explanation to that.
Inside InputFilter, I need a way to detect a new-line character insertion command, then replace this character by an empty string.
Any help will be very appreciated!