好的,我有一个EditText
, A Search Button
And A Textview
带有大的可滚动文本。
当我输入一个单词时EditText
并按Search Button
, 该词被突出显示.到目前为止,一切正常。
但我想要的是,当我输入一个单词并按button
,它应该突出显示该单词,并将相机移动到该特定单词(我认为这在 android 中称为聚焦),我不想每次都滚动以找到我突出显示的单词。假设我键入的单词位于该单词的底部文本并突出显示,我无法每次滚动找到它,这很烦人。所以请帮忙!
所以我想要的是,当我搜索一个词时,它应该得到
突出显示,并且也应该自动出现在屏幕上,无需
我向下或向上滚动。
还有一件事,我想在 EditText 后立即取消突出显示该单词
变空
这是我的代码
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.xeoh.android.texthighlighter.TextHighlighter;
public class MainActivity extends AppCompatActivity {
EditText search;
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
search = (EditText)findViewById(R.id.search);
textView = (TextView) findViewById(R.id.text);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new TextHighlighter()
.setBackgroundColor(Color.parseColor("#FFFF00"))
.setForegroundColor(Color.RED)
.addTarget(textView)
.highlight(search.getText().toString(),TextHighlighter.BASE_MATCHER);
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/search"
android:hint="Search"
android:layout_marginTop="20dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search"
android:id="@+id/button"/>
<TextView
android:layout_width="match_parent"
android:id="@+id/text"
android:textSize="25sp"
android:layout_height="wrap_content"
android:text="@string/test"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
假设您有:
Layout:
<Button
android:id="@+id/button"
android:text="search!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ScrollView
android:id="@+id/textViewWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/scrollableText"
android:textIsSelectable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
然后在你的代码中:
scrollableText = (TextView) findViewById(R.id.scrollableText);
editText = (EditText) findViewById(R.id.editText);
textViewWrapper = (ScrollView) findViewById(R.id.textViewWrapper);
button = (Button) findViewById(R.id.button);
scrollableText.setText(R.string.longText);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String criteria = editText.getText().toString();
String fullText = scrollableText.getText().toString();
if (fullText.contains(criteria)) {
int indexOfCriteria = fullText.indexOf(criteria);
int lineNumber = scrollableText.getLayout().getLineForOffset(indexOfCriteria);
String highlighted = "<font color='red'>"+criteria+"</font>";
fullText = fullText.replace(criteria, highlighted);
scrollableText.setText(Html.fromHtml(fullText));
textViewWrapper.scrollTo(0, scrollableText.getLayout().getLineTop(lineNumber));
}
}
});
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.length() == 0) {
String fullText = scrollableText.getText().toString();
fullText = fullText.replace("<font color='red'>", "");
fullText = fullText.replace("</font>", "");
scrollableText.setText(fullText);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
where longText
是你定义的一些长文本strings.xml
。您可能想在文本上应用一些其他样式(就像我所做的那样)<font color='red'>
).
它应该可以完成这项工作。我已经在 android 8 (Oreo) 上进行了测试,效果很好。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)