我正在为Android编写一个应用程序,我正在尝试将部分字符串超链接到应用程序中的另一个页面,但我正在努力解决该怎么做。
我已经设法使用这样的整个文本视图:
TextView txtVirus = (TextView) findViewById(R.glossaryMalwareID.txtVirus);
txtVirus.setText(Html.fromHtml("<U>Computer Virus<U>"));
txtVirus.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.digitalsecurity.app.GLOSSARYVIRUS"));
}
});
然而这意味着字符串必须在一个完整的新行上(或者我相信)。
我想要的是能够像下面代码中的注释所示:
TextView txtVirus = (TextView) findViewById(R.glossaryMalwareID.virusmain);
txtVirus.setText(Html.fromHtml("<br>" +
"A Computer Virus is a small peice of " +
"Software" +//i want the word 'software' hyperlinked to another page in my program
"so on and so on"));
答案 0 :(得分:1)
TextView txtVirus = (TextView) findViewById(R.glossaryMalwareID.virusmain);
// this is the text we'll be operating on
SpannableString text = new SpannableString("A Computer Virus is a small peice of Software. to make this easier to");
//stops it from flickering to another colour when string is pressed.
text.setSpan(new ForegroundColorSpan(Color.WHITE), 0, 69, 0);
// make "Software" (characters 37 to 45)
//runs when clickableSpan is pressed
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View view) {
startActivity(new Intent ("com.digitalsecurity.app.MALWAREHOWTOPROTECT"));
}
};
//this is what calls the method
text.setSpan(clickableSpan, 37, 45, 0);
//underlines it
text.setSpan(new URLSpan(""), 37, 45, 0);
//turns it blue
text.setSpan(new ForegroundColorSpan(Color.BLUE), 37, 45, 0);
txtVirus.setMovementMethod(LinkMovementMethod.getInstance());
// shove our styled text into the TextView
txtVirus.setText(text, BufferType.SPANNABLE);
我从这里得到了帮助,还有其他相当有用的格式化选项:
http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring
我希望它可以帮助其他需要相同的人
答案 1 :(得分:0)
对于TextView,如果显示链接,则Textview
如下所示:
TextView txtVirus = (TextView) findViewById(R.glossaryMalwareID.virusmain);
txtVirus .setText("A Computer Virus is a small peice of " +
"Software" + just write down your link automatically converted to URL
"so on and so on);
Linkify.addLinks(noteView, Linkify.ALL);
如果您有任何疑问,请与我们联系..