android - txt文件中的可点击单词
问题描述
我使用以下代码从 textview 中的文件夹中读取了一个 .txt 文件:
String txt = "";
StringBuffer sbuffer1 = new StringBuffer();
InputStream is = this.getResources().openRawResource(R.raw.file);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
while ((txt = reader.readLine()) !=null){
sbuffer1.append(txt + "\n");
}
textview.setText(sbuffer1);
is.close();
}catch(Exception e) {
e.printStackTrace();
}
.txt 文件包含很长的文本,我希望文本中的特定单词可以点击,当点击特定单词时,我想显示一条 toast 消息。我怎样才能做到这一点?
这是 spannablestring 的示例代码,但我不知道如何在我的代码案例中应用它:
String text = " what do I put here??";
SpannableString ss = new SpannableString(text);
ClickableSpan clickableSpan1 = new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "One", Toast.LENGTH_SHORT).show();
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(Color.BLUE);
ds.setUnderlineText(false);
}
};
ClickableSpan clickableSpan2 = new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "Two", Toast.LENGTH_SHORT).show();
}
};
ss.setSpan(clickableSpan1, 7, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(clickableSpan2, 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
解决方案
尝试这个:
String txt = "";
StringBuilder sbuffer1 = new StringBuilder();
InputStream is = this.getResources().openRawResource(R.raw.file);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
while ((txt = reader.readLine()) !=null){
sbuffer1.append(txt).append("\n");
}
is.close();
}catch(Exception e) {
e.printStackTrace();
}
String text = sbuffer1.toString();
SpannableStringBuilder builder = new SpannableStringBuilder(text);
ClickableSpan clickable0 = new ClickableSpan() {
@Override
public void onClick(View textView) {
Toast.makeText(MainActivity.this, "word0", Toast.LENGTH_LONG).show();
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(Color.BLUE);
ds.setUnderlineText(true);
}
};
ClickableSpan clickable1 = new ClickableSpan() {
@Override
public void onClick(View textView) {
Toast.makeText(MainActivity.this, "word1", Toast.LENGTH_LONG).show();
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(Color.BLUE);
ds.setUnderlineText(true);
}
};
String word0 = "word0";
String word1 = "word1";
builder.setSpan(clickable0, text.indexOf(word0), text.indexOf(word0) + word0.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.setSpan(clickable1, text.indexOf(word1), text.indexOf(word1) + word1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText(builder);
textview.setMovementMethod(LinkMovementMethod.getInstance());
上面的代码是 2 个单词,你可以随意更改它们。我希望我没有错别字
推荐阅读
- javascript - JavaScript Array.length 在函数中为 2,但在另一个函数中为 0
- vue.js - 在 console.log 中未定义但 vue 控制台显示正确的值
- sbt - build.sbt 如何解析为 AWS codeartifact repo?
- stata - 基于现有字符串变量生成字符串变量
- html - 如何仅使用嵌入脚本构建在另一个应用程序中显示的 Web 应用程序
- microsoft-graph-api - Microsoft Graph API 邮件中附件的 3 MB 限制是累积的吗?
- python - 得到“警告:找不到输入匹配:加载目标 b”,所有文件都在同一目录中
- excel - 索引并匹配多行多列 - 返回下面的单元格
- debugging - 如何调试小部件 Xcode 12
- javascript - 用onclick JS函数插入表格