Using Android:Digits attribute to restrict characters stops action next button working
Asked Answered
B

2

13

I have a number of EditText fields on an Android app and I've used android:digits to stop invalid characters being input.

eg: android:digits="@string/validchars" (where "validchars" is a string resource of all the valid characters - eg A-Z, 0-9, and some punctuation )

The problem is, if the user hits the ENTER key on the soft keyboard it used to take them to the next field but since I added android:digits bit this has stopped working. I've tried adding \n to the validchars but this just allows Carriage Returns in the single line textboxes (!) so doesn't help. I've tried fiddling with the imeOptions (adding actionNext etc) but this doesn't seem to fix it either. I've read about TextWatchers and InputFilters but both seem to be over-complicated for what I'm trying to do and the android:digits bit works fine in stopping the invalid characters.

Has anyone got any ideas?

I'm using Android SDK version 8 and targeting both 2.2 and 2.3.5 devices.

Broadcast answered 15/6, 2012 at 9:27 Comment(0)
B
25

After a bit of poking around with the attributes available for an EditText I found that when I add android:singleLine="true" the ENTER key now moves the focus to the next field, even when a android:digits is restricting the characters entered. Problem solved! (for me, anyway!)

Broadcast answered 9/7, 2012 at 8:4 Comment(4)
Alternatively, if the user desires to have a "done" option instead of next, see this question, which is related #1920242Archive
android:singleLine deprecated =( is it any other way?Predecease
I believe you should now use maxLines="1"Broadcast
but android:maxLines= 1 doesnt work well with android:digitsNarrows
C
0

Just add "\n" in digits attribute as allow enter key and textMultiLine as inputType

android:inputType="textMultiLine"      android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 \n"
Collie answered 14/6, 2023 at 12:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.