How to search and Highlight Text within an EditText

随声附和 提交于 2019-12-08 06:38:00

问题


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:


  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:

  • http://developer.android.com/reference/java/lang/String.html
  • http://developer.android.com/reference/java/util/regex/package-summary.html


来源:https://stackoverflow.com/questions/3030972/how-to-search-and-highlight-text-within-an-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!