如何搜索和一个EditText中突出显示文本文本、突出、EditText

2023-09-09 21:07:07 作者:早知是梦

我已经搜查高和低的东西,这似乎是一个简单的任务。原谅我,我来从其他编程语言Android和是新来这个平台和Java。

我想要做的就是创建一个对话框弹出,其中用户输入要搜索的文本和code将采取该文本,并在一个EditText控件中的所有文本中搜索它,如果它的发现是什么,高亮显示。

我以前做过这个,例如在VB中,它出了类似这样的伪code的内容:

抓住从(EditText上),文本将其分配给一个字符串搜索该字符串的长度(每个字符)的子字符串,如果找到返回字符串内子的位置(索引)。如果找到,启动(EditText上).setSelection亮点开始对长度返回的位置

这是否有道理?

我只是想搜索的EditText并发现时,滚动到它,它就会被突出显示。也许有什么东西在的Andr​​oid / Java的相当于什么,我需要在这里?

任何帮助/指针将大大AP preciated

解决方案     抓住从(EditText上),文本将其分配给一个字符串  

尝试下面的code样品:

 的EditText inputUsername =(EditText上)findViewById(R.id.manageAccountInputUsername);inputUsername.getText()。的toString() 

^^与正在使用的ID替换的ID。

在此之后,你有标准的字符串的方法可用。您也可以使用正则表达式一个复杂的搜索查询:

http://developer.android.com/reference/java/lang/ String.html http://developer.android.com/reference/的Java / UTIL /正则表达式/包summary.html

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.

word2010查找并突出显示文本的方法

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 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

解决方案

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