如何搜索和一个EditText中突出显示文本 [英] How to search and Highlight Text within an EditText

查看:110
本文介绍了如何搜索和一个EditText中突出显示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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


  1. 抓住从(EditText上),文本将其分配给一个字符串

  2. 搜索该字符串的长度(每个字符)的子字符串,如果找到返回字符串内子的位置(索引)。

  3. 如果找到,启动(EditText上).setSelection亮点开始对长度
  4. 返回的位置

这是否有道理?

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

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


解决方案

  

      
  1. 抓住从(EditText上),文本将其分配给一个字符串

  2.   

尝试下面的code样品:<​​/ P>

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

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

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

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

这篇关于如何搜索和一个EditText中突出显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆