I have an Android activity that has a text edit field, considering that the user has something "empty" (3 max) Entering text is the blank space inserted using the given function:
Insert public zeros (see footage) {Edit text editText = (edit text) Find ViewById (R.id .editText1); Int lastIndex = 0; Int count = 0; While (last index! = -1) {last index = edit text.gate text (). ToString () Indexoff ("______", last index); If (last indicator! = -1) {count ++; LastIndex + = "______" length () .; }} If (calculation & lt; = 2) {editText.append ("______"); } And {toast} GetBaseContext, "Maximum 3 Acceptable Permissions", Toast. LNNHT_Leng) Show (); }}
What do I want, as the user performs backspace or editing text, "blank" should be treated as a character. So
UPDATE
So with the help of the answer, I got this job almost. I added the addTextChangedListener to the on-crat and the following code is running. It replaces all the underscores in the string and not only on the current word.
txt.addTextChangedListener (overwrite public auction at {@TextChanged} (before Charsconquans s, int start, int, before) {// Tudo auto-generated method stub Spannable textSpan = txt .gettext (); Final integer selection = txt.getSelectionStart (); Last pattern pattern = Pattern.compile ("\\ w +") Last Matcher matcher = pattern.matcher (textSpan); int end = 0; String currentWord = null; while (matcher.find ()) {start = matcher.start (); end = matcher.end (); if (start & lt; = selection & amp; amp; selection & lt; = End) {currentWord = textSpan.SubSequence start, end) .STROSTING (); If (current Word.contains ("_")) {string full text = txt.getText (). ToString (); Toast. Make text (getApplicationContext), current word + ", toast .LENGTH_SHO RT) .show (); full text = whole text. Location (current word," "); txt.setText (wholeText); txt.setSelection (.txt Overload {// TODO auto-generated method stub} @TextChanged (editing) before editing .getText () Length ());}}}} @TextChanged (After effect, s, int count, int after) Qualified) after override public zero {// TODO auto-generated method stub}});
Your best bet is Enter a textwatcher on the field and search for changes in it. After the text changes the spaces and remove the entire blank if necessary.
No comments:
Post a Comment