突出的TextView使用的EditText [英] Highlight Textview Using EditText
本文介绍了突出的TextView使用的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
林目前正在应用类似Android的搜索引擎,我想重点介绍的EditText搜索的词来TextView的...这是我有这么远,只突出了第一个单词在TextView中
TV.setText(Hello World的,TextView.BufferType.SPANNABLE);
Spannable WordtoSpan =(Spannable)TV.getText();
WordtoSpan.setSpan(新BackgroundColorSpan(0xFFFFFF00),0,notes.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TV.setText(WordtoSpan);
解决方案
我想你想突出的TextView其用户在一个EditText上的一个特定的字。 说等是您的EditText和电视是TextView的对象。使用下面的code:
字符串ETT = et.getText()的toString()。
字符串TVT = tv.getText()的toString()。
INT指数= tvt.indexOf(ETT);
Spannable WordtoSpan =新SpannableString(tv.getText());
如果(索引!= -1)
{
WordtoSpan.setSpan(新BackgroundColorSpan(0xFFFFFF00),索引,索引+ ett.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(WordtoSpan,TextView.BufferType.SPANNABLE);
}
其他
tv.setText(我们国家的名称是孟加拉);
下面是结果:
下面是完整的code:
公共类MotivationalQuotesActivity延伸活动{
/ **第一次创建活动时调用。 * /
按钮旁边;
的EditText等;
TextView的电视;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
等=(EditText上)findViewById(R.id.et);
电视=(TextView中)findViewById(R.id.tv);
tv.setText(我们国家的名称是孟加拉);
接下来=(按钮)findViewById(R.id.button1);
next.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
// TODO自动生成方法存根
。字符串ETT = et.getText()的toString();
字符串TVT = tv.getText()的toString()。
INT指数= tvt.indexOf(ETT);
Spannable WordtoSpan =新SpannableString(tv.getText());
如果(索引!= -1)
{
WordtoSpan.setSpan(新BackgroundColorSpan(0xFFFFFF00),索引,索引+ ett.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(WordtoSpan,TextView.BufferType.SPANNABLE);
}
其他
tv.setText(我们国家的名称是孟加拉);
}
});
}
}
Im currently making a search engine like application for android and i want to highlight the searched word from edittext to textview... this is that i got so far and it only highlights the first word in the textview
TV.setText("Hello World", TextView.BufferType.SPANNABLE);
Spannable WordtoSpan = (Spannable) TV.getText();
WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), 0, notes.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TV.setText(WordtoSpan);
解决方案
I think you want to highlight a specific word of TextView which user types in a EditText. Say et is your EditText and tv is TextView object. Use the following code:
String ett =et.getText().toString();
String tvt =tv.getText().toString();
int index = tvt.indexOf(ett);
Spannable WordtoSpan = new SpannableString( tv.getText() );
if(index != -1)
{
WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), index, index+ett.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(WordtoSpan, TextView.BufferType.SPANNABLE);
}
else
tv.setText("The name of our country is Bangladesh");
Here is the outcome:
Here is the complete code:
public class MotivationalQuotesActivity extends Activity {
/** Called when the activity is first created. */
Button next;
EditText et;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) findViewById(R.id.et);
tv = (TextView) findViewById(R.id.tv);
tv.setText("The name of our country is Bangladesh");
next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String ett =et.getText().toString();
String tvt =tv.getText().toString();
int index = tvt.indexOf(ett);
Spannable WordtoSpan = new SpannableString( tv.getText() );
if(index != -1)
{
WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), index, index+ett.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(WordtoSpan, TextView.BufferType.SPANNABLE);
}
else
tv.setText("The name of our country is Bangladesh");
}
});
}
}
这篇关于突出的TextView使用的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文