TextView textView = (TextView) this.findViewById(R.id.textview); TextView textView2 = (TextView) this.findViewById(R.id.textview2); String text1 = "显示Activity1"; String text2 = "显示Activity2"; // 主要是用来拆分字符串 SpannableString spannableString = new SpannableString(text1); SpannableString spannableString2 = new SpannableString(text2); spannableString.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { // TODO Auto-generated method stub Intent intent = new Intent(Main.this, Activity1.class); startActivity(intent); } }, 0, text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //SPAN_EXCLUSIVE_EXCLUSIVE从字符串0-text1.length任意位置触发点击事件 spannableString2.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { // TODO Auto-generated method stub Intent intent = new Intent(Main.this, Activity2.class); startActivity(intent); } }, 0, text2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(spannableString); textView2.setText(spannableString2); textView.setMovementMethod(LinkMovementMethod.getInstance()); textView2.setMovementMethod(LinkMovementMethod.getInstance()); } 运行结果是:
时间: 2024-11-06 13:37:20