I've searched high and low for something that seems to be a simple task. Forgive me, I am coming to Android from other programming languages and am new to this platform and Java.
What I want to do is create a dialog pop-up where a user enters text to search for and the code would take that text and search for it within all the text in an EditText control and if it's found, highlight it.
I've done this before, for example in VB and it went something similar to this pseudo code:
- grab the text from the (EditText) assign it to a string
- search the length of that string (character by character) for the substring, if it's found return the position (index) of the substring within the string.
- if found, start the (EditText).setSelection highlight beginning on the returned position for the lengt开发者_开发百科h of
Does this make sense?
I just want to search a EditText for and when found, scroll to it and it'll be highlighted. Maybe there's something in Android/Java equivalent to what I need here?
Any help / pointers would be greatly appreciated
- grab the text from the (EditText) assign it to a string
Try the code sample below:
EditText inputUsername = (EditText) findViewById(R.id.manageAccountInputUsername);
inputUsername.getText().toString()
^^ Replace the IDs with the IDs you are using.
After this, you have the standard string methods available. You could also use Regex for a complex search query:
- http://developer.android.com/reference/java/lang/String.html
- http://developer.android.com/reference/java/util/regex/package-summary.html
精彩评论