gpt4 book ai didi

java - 从预生成的 html 添加突出显示到 span

转载 作者:行者123 更新时间:2023-12-01 04:57:52 26 4
gpt4 key购买 nike

您好,我不知道如何使用 styledText 字符串中的跨度并仅向这些跨度中的单词添加突出显示。当前的 styledText 字符串是我从另一个应用程序收到的代码的示例。跨度的突出显示需要自动发生。有谁知道如何最好地做到这一点?

    public class ArticleActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article);

TextView textview = new TextView(this);

String styledText = "Vanwege een verdacht koffertje <span id=\"txt8264:idx8:tkn41:cls1\" class=\"supported dictionary\">is</span> vanochtend een tankstation langs <span id=\"txt8264:idx18:tkn4:cls11\" class=\"supported dictionary\">de</span> A16 bij Hazeldonk korte tijd afgesloten <span id=\"txt8264:idx32:tkn1156:cls1\" class=\"supported dictionary\">geweest</span>.<br/><br/>Volgens <span id=\"txt8264:idx37:tkn4:cls11\" class=\"supported dictionary\">de</span> politie stapte een man uit zijn auto en zette hij <span id=\"txt8264:idx59:tkn98:cls11\" class=\"supported dictionary\">het</span> koffertje naast <span id=\"txt8264:idx65:tkn98:cls11\" class=\"supported dictionary\">het</span> tankstation neer. Daarna reed hij weg.<br/><br/>Een explosievenverkenner van <span id=\"txt8264:idx87:tkn4:cls11\" class=\"supported dictionary\">de</span> politie onderzocht <span id=\"txt8264:idx93:tkn98:cls11\" class=\"supported dictionary\">het</span> koffertje en constateerde dat <span id=\"txt8264:idx103:tkn98:cls11\" class=\"supported dictionary\">het</span> leeg was. <span id=\"txt8264:idx110:tkn66:cls11\" class=\"supported dictionary\">Het</span> tankstation werd korte tijd later weer vrijgegeven. <span id=\"txt8264:idx127:tkn25:cls11\" class=\"supported dictionary\">De</span> afsluiting <span id=\"txt8264:idx131:tkn404:cls1\" class=\"supported dictionary\">had</span> geen gevolgen voor <span id=\"txt8264:idx139:tkn98:cls11\" class=\"supported dictionary\">het</span> verkeer op <span id=\"txt8264:idx145:tkn4:cls11\" class=\"supported dictionary\">de</span> A16 bij <span id=\"txt8264:idx151:tkn4:cls11\" class=\"supported dictionary\">de</span> grensovergang met België.<br/><br/>Ook <span id=\"txt8264:idx162:tkn4:cls11\" class=\"supported dictionary\">de</span> parkeerplaats achter <span id=\"txt8264:idx168:tkn98:cls11\" class=\"supported dictionary\">het</span> tankstation <span id=\"txt8264:idx172:tkn215:cls1\" class=\"supported dictionary\">was</span> bereikbaar.<br/><br/>";

textview.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);

setContentView(textview);
}
}

最佳答案

我发现最好/唯一的工作方法是使用带有内联 html/css 的 webview。

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = new WebView(this);
String styledText = "Vanwege een verdacht koffertje <span id=\"txt8264:idx8:tkn41:cls1\" class=\"supported dictionary\">is</span> vanochtend een tankstation langs <span id=\"txt8264:idx18:tkn4:cls11\" class=\"supported dictionary\">de</span> A16 bij Hazeldonk korte tijd afgesloten <span id=\"txt8264:idx32:tkn1156:cls1\" class=\"supported dictionary\">geweest</span>.<br/><br/>Volgens <span id=\"txt8264:idx37:tkn4:cls11\" class=\"supported dictionary\">de</span> politie stapte een man uit zijn auto en zette hij <span id=\"txt8264:idx59:tkn98:cls11\" class=\"supported dictionary\">het</span> koffertje naast <span id=\"txt8264:idx65:tkn98:cls11\" class=\"supported dictionary\">het</span> tankstation neer. Daarna reed hij weg.<br/><br/>Een explosievenverkenner van <span id=\"txt8264:idx87:tkn4:cls11\" class=\"supported dictionary\">de</span> politie onderzocht <span id=\"txt8264:idx93:tkn98:cls11\" class=\"supported dictionary\">het</span> koffertje en constateerde dat <span id=\"txt8264:idx103:tkn98:cls11\" class=\"supported dictionary\">het</span> leeg was. <span id=\"txt8264:idx110:tkn66:cls11\" class=\"supported dictionary\">Het</span> tankstation werd korte tijd later weer vrijgegeven. <span id=\"txt8264:idx127:tkn25:cls11\" class=\"supported dictionary\">De</span> afsluiting <span id=\"txt8264:idx131:tkn404:cls1\" class=\"supported dictionary\">had</span> geen gevolgen voor <span id=\"txt8264:idx139:tkn98:cls11\" class=\"supported dictionary\">het</span> verkeer op <span id=\"txt8264:idx145:tkn4:cls11\" class=\"supported dictionary\">de</span> A16 bij <span id=\"txt8264:idx151:tkn4:cls11\" class=\"supported dictionary\">de</span> grensovergang met België.<br/><br/>Ook <span id=\"txt8264:idx162:tkn4:cls11\" class=\"supported dictionary\">de</span> parkeerplaats achter <span id=\"txt8264:idx168:tkn98:cls11\" class=\"supported dictionary\">het</span> tankstation <span id=\"txt8264:idx172:tkn215:cls1\" class=\"supported dictionary\">was</span> bereikbaar.<br/><br/>";
final WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
StringBuilder html = new StringBuilder();
html.append("<html>");
html.append("<head>");
html.append("<style type='text/css'> body .supported{background-color:#00A4E4;} </style>");
html.append("</head>");
html.append("<body>");
html.append("<h1>Some Heading</h1>");
html.append(styledText.toString());
html.append("</body></html>");
webView.loadDataWithBaseURL("file:///android_asset/", html.toString(), "text/html", "UTF-8", "");
setContentView(webView);
}

关于java - 从预生成的 html 添加突出显示到 span,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13805435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com