I do backspace in textfield, and i need to set as composingText word or it part, that has left before cursor position. I must do that thing each time when i press backspace. So my step must be:
get size of last word (or part of it);
make it as composing text.
I try do it like this:
ic.setSelection(startWordPosition, word.length());
ic.setComposingText(word, 1);
All works good, while i don't test backspace in string like "a.a". It select all 3 characters, but when set, replace only part after dot.
Then i try make it like this:
ic.deleteSurroundingText(word.length(),0);
ic.setComposingText(word, 1);
But it not work correctly and delete sometimes text before my word(maybe because it currently compose).
Just find one more way that looks like right:
ic.finishComposingText();
ic.deleteSurroundingText(word.length(),0);
ic.setComposingText(word, 1);
ic.setSelection(fullInputedString.length(),fullInputedString.length());
Is it correct?
How should it be in right way?
target sdk=8
Thanks!
UPD:
I found next solution for
ic.finishComposingText();
ic.deleteSurroundingText(wordForInsert.length(), 0);
ic.setComposingText(wordForInsert, 1);
And for latest android version i use appropriate method:
ic.setComposingRegion();
But this solution provide issue in standatrd android browser for 4.0v Android and latest.
Related
For example: if(checkInputs(email, username, password|))
If I finish type the word password and the cursor (|) is at that position.
Is there anyway to start a curvy bracket at the very end without have to press right arrow twice?
And what if I needed to put a ; to finish the line. Is there a way through a shortcut to do it faster?
I think you're looking for the "Complete statement" shortcut. This will add closing parentheses, brackets, semicolons, etc. By default it is bound to Control + Shift + Enter or Command + Shift + Enter.
Check the list of shortcuts here: https://developer.android.com/studio/intro/keyboard-shortcuts.html
You can use the end button (the one that says end).
Using end sends the cursor to the end of the line, and using home sends the cursor to the front of the line. It works no matter where in the line you are, assuming it isn't at the end of the line already (because that would of course not do anything because it's already there).
Also note that if there is a comment at the end of the actual code (still same line) it jumps to the end of the comment instead.
For mac it's different. It's the same idea (untested though, I don't have mac). There you press Function + [Arrow left/arrow right]
I'm working on an application where need to complete date automatic/suggest while writing. Thing is user can type other text as well so how to sure when he start writing for date.
For reference: I have found similar feature on slack app.
Any suggestion?
Add TextWatcher to the EditText view. In one of it's methods for example beforeTextChanged or onTextChanged get the last entered word (or char sequence - it's up to you to decide which part of entered text you want to check). Apply regular expression to it to check if it looks like date or not. Again it's up to you to decide what looks like date and what is not. If it looks like date - provide users with UI for auto completion or autocomplete by our own. Don't forget to move cursor to the end of autocompleted text using EditText.setSelection() method.
Actually I want to delete all text in a text field, and I am using a loop calling device.press('KEYCODE_DEL') to achieve this.
But there are two disadvantages:
Not efficient enough, especially when I don't know how many
characters in the text field so I need set a large enough loop
Need to move the cursor to the end before deleting
So I am trying to accomplish this by two steps:
select all text
press delete button
I found an similar question here which is not solved yet.
And there is an answer for how to select all text, but I think it has the same issues as my loop delete way.
I did several tests, and found a way close to it:
device.press('KEYCODE_MENU', 'MonkeyDevice.DOWN', '')
device.press('KEYCODE_A')
device.press('KEYCODE_MENU', 'MonkeyDevice.UP', '')
I thought these three steps accomplish a MENU+A operation. But it did not work every time. I executed this code for 20 times(in a loop) and found it only took effect for about 5-8 times.
Besides, I found these three steps will move the cursor to the first place most of the time.
Did anyone know why is this operation not reliable? Or any other suggestions to select all text?
Appreciate for any suggestions!
AndroidViewClient's EditText has a method for that:
def setText(self, text):
"""
This function makes sure that any previously entered text is deleted before
setting the value of the field.
"""
if self.text() == text:
return
self.touch()
guardrail = 0
maxSize = len(self.text()) + 1
while maxSize > guardrail:
guardrail += 1
self.device.press('KEYCODE_DEL', adbclient.DOWN_AND_UP)
self.device.press('KEYCODE_FORWARD_DEL', adbclient.DOWN_AND_UP)
self.type(text, alreadyTouched=True)
Using AndroidViewClient its pretty easy. Try this code-
editText= vc.findViewByIdOrRaise(EDITTEXT ID in quotes)
(x,y) = editText.getXY()
editText.device.drag((x,y), (x,y), 2000, 1)
vc.dump()
device.press('KEYCODE_DEL')
I am now working on a calculator, and everything works fine except for decimal places.
The calculator contains 2 displays actually, one is called fakedisplay for actual operations, and one is called Display, for presenting the desired format, ie adding commas.
When pressing 12345.678, Display will follow fakedisplay and present as 12,345.678, but if i press 12345.009, the fakedisplay will work normally as 12345.009, but the Display stuck as 12,345 until 9 is pressed, and at that time it will show 12,345.009 normally.
However, it is strange that when the user presses 0, there is no response, and until pressing 9, 009 will then immediately append.
I know this arise from the parsing code, but based on this, how could I amend the following code? I really cannot think of any solution... Many thanks for all your advice!
one.setOnClickListener(new View.OnClickListener() {
if (str.length()<15) {
Fakedisplay.append("1");
}
DecimalFormat myFormatter1 = new DecimalFormat("###,###,###,###.#################");
String str1=Fakedisplay.getText().toString();
String stripped1 = Double.valueOf(str1).toString();
stripped1 = myFormatter1.format(Double.valueOf(stripped1));
if (stripped1.endsWith(".0"))
stripped1 = stripped1.substring(0, stripped1.length() - 2);
Display.setText(stripped1);
}
Probably the easiest solution is to not strip off the .0 in the code for every keystroke..
Instead, only strip off trailing zeros (assuming there's a decimal point in there of course) when the user calls for a result. Entering keys such as the digit keys 0 through 9, the decimal point ., or the sign-change key +/- (what I'll call the entry keys) are not generating a result so should not strip trailing zeros.
However, non-entry keys, such as when you press + or - or = on your calculator can freely modify the number.
That will give you a display of the digits being entered as the user enters them but will still strip off trailing zeros when necessary.
You can do that with a modification to your statement (and, as mentioned, only doing this when the user presses a non-entry key):
stripped1 = stripped1.replaceAll("(\\.[0-9]*[1-9])0+$","$1");
stripped1 = stripped1.replaceAll("\\.0$","");
The first statement removes all trailing zeros at the end of a decimal number (other than on if it's really an integer). The second takes care of that case.
No doubt I could make a single substitution if I gave it some more thought but that should be enough to get it functional.
I want to make a smart keyboard that can learn and save new words from user. I already made note and keyboard separately, the problem is :
how to read all keystrokes and write it to my note in background?
how to save my note automatically?
thanks for your help
Keep a String or StringBuilder that stores all the text that the user types. All text sent through your soft keyboard will have to pass through the onKey method.
So, I'd do something like this:
1) In onKey, check to make sure primaryCode (the keycode that was pressed) is a letter/number/apostrophe using the corresponding functions. So, something like
Character.isDefined(primaryCode)
2) Concatenate primaryCode onto the end of your StringBuilder/String.
You'll also have to deal with the user moving the cursor/backspacing. In my keyboard, I only store the most recent two words (resetting this whenever the user moves the cursor). That way, the keyboard can learn what the most likely word is given the last word.
You can save your "note" using an ObjectOutputStream or (if it's fairly small) using sharedPreferences.
Send me an email if you run into an more issues: I've been writing a soft keyboard for a while so I'm pretty familiar with it.