如何解决在textview中搜索长文本的单词
这是我的Java代码,我的textview中有一个大文本,我想搜索单词并将其突出显示,这样就很容易找到bt,我不想在输入编辑文本后立即创建按钮它应该突出显示该词。 这是我所做的工作,直到我在编辑文本中输入一些单词,但我希望突出显示的单词能帮助我
public class sample extends AppCompatActivity implements {
TextView textView;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle("sample");
if (getIntent().hasExtra("a1")) {
Stribg data= getIntent().getStringExtra("a1");
TextView textView = findViewById(R.id.Text1);
textView.setText(data);
} else if (getIntent().hasExtra("a2")) {
String data = getIntent().getStringExtra("a2");
TextView textView = findViewById(R.id.Text1);
textView.setText(data);
} else if (getIntent().hasExtra("a3")) {
String data = getIntent().getStringExtra("a3");
TextView textView = findViewById(R.id.Text1);
textView.setText(data);
} else if (getIntent().hasExtra("a4")) {
String data = getIntent().getStringExtra("a4");
TextView textView = findViewById(R.id.Text1);
textView.setText(data);
} else if (getIntent().hasExtra("a5")) {
String data = getIntent().getStringExtra("a5");
TextView textView = findViewById(R.id.Text1);
textView.setText(data);
} else if (getIntent().hasExtra("a6")) {
String data = getIntent().getStringExtra("a6");
TextView textView = findViewById(R.id.Text1);
textView.setText(data);
} else if (getIntent().hasExtra("a7")) {
String data = getIntent().getStringExtra("a7");
TextView textView = findViewById(R.id.Text1);
textView.setText(data);
} else if (getIntent().hasExtra("a8")) {
String data = getIntent().getStringExtra("a8");
TextView textView = findViewById(R.id.Text1);
textView.setText(data);
} else if (getIntent().hasExtra("a9")) {
String data = getIntent().getStringExtra("a9");
TextView textView = findViewById(R.id.Text1);
textView.setText(data);
} else if (getIntent().hasExtra("a10")) {
String data = getIntent().getStringExtra("a10");
TextView textView = findViewById(R.id.Text1);
textView.setText(data);
} else if (getIntent().hasExtra("a11")) {
String data = getIntent().getStringExtra("a11");
TextView textView = findViewById(R.id.Text1);
textView.setText(data);
} else if (getIntent().hasExtra("a12")) {
String data = getIntent().getStringExtra("a12");
TextView textView = findViewById(R.id.Text1);
textView.setText(data);
} else if (getIntent().hasExtra("a13")) {
String data = getIntent().getStringExtra("a13");
TextView textView = findViewById(R.id.Text1);
textView.setText(data);
} else if (getIntent().hasExtra("a14")) {
String data = getIntent().getStringExtra("a14");
TextView textView = findViewById(R.id.Text1);
textView.setText(data);
} else if (getIntent().hasExtra("a15")) {
String data = getIntent().getStringExtra("a15");
TextView textView = findViewById(R.id.Text1);
textView.setText(data);
} else if (getIntent().hasExtra("a16")) {
String data = getIntent().getStringExtra("a16");
TextView textView = findViewById(R.id.Text1);
textView.setText(data);
} else if (getIntent().hasExtra("a17")) {
String data = getIntent().getStringExtra("a17");
TextView textView = findViewById(R.id.Text1);
textView.setText(data);
} else if (getIntent().hasExtra("a18")) {
String data = getIntent().getStringExtra("a18");
TextView textView = findViewById(R.id.Text1);
textView.setText(data);
}
public void onClick(View view) {
String textToHighlight=editText.getText().toString();
String replaceWith="<span style='background-color:yellow'>"+textToHighlight+"</span>";
String originalText = textView.getText().toString();
String modifiedtext= originalText.replaceAll(textToHighlight,replaceWith);
textView.setText(Html.fromHtml(modifiedtext));
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。