我想更改文件中某些相同单词的颜色和字体
private void readCode(String filename) throws Exception{
File filesDir=
(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_DOWNLOADS));
File myFile=new File(filesDir,filename);
BufferedReader br = new BufferedReader(new FileReader(myFile));
String st;
StringBuilder code= new StringBuilder("hello");
while ((st = br.readLine()) != null)
{
SpannableString stt= new SpannableString(st);
if(st.contains("import")){
int index=st.indexOf("import");
stt.setSpan(new ForegroundColorSpan(Color.RED), index,index+6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
code.append(stt);
code.append(System.getProperty("line.separator"));}
myCOde.setText(code);
}
此代码不会更改文本的颜色,而只是将纯文本设置为文本视图