How to search and Highlight Text within an EditTex

2019-06-06 08:18发布

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:

  1. grab the text from the (EditText) assign it to a string
  2. 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.
  3. if found, start the (EditText).setSelection highlight beginning on the returned position for the length 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

1条回答
干净又极端
2楼-- · 2019-06-06 09:04
  1. 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:

查看更多
登录 后发表回答