TextView 超链接点击跳转到下一个Activity

1:activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tv_show_next_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</RelativeLayout>

2:MainActivity.java

public class MainActivity extends Activity {
private TextView tvNextActivity=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvNextActivity=(TextView)findViewById(R.id.tv_show_next_activity);
String info="Next activity";

SpannableString sps=new SpannableString(info);

//对字符串拆分并注册点击事件
sps.setSpan(new ClickableSpan(){
public void onClick(View view){
Intent intent=new Intent(MainActivity.this,NextActivity.class);
startActivity(intent);
}
}, 0, info.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

tvNextActivity.setText(sps);
tvNextActivity.setMovementMethod(LinkMovementMethod.getInstance());
}

}

3:运行结果:

TextView 超链接点击跳转到下一个Activity

 

你可能感兴趣的:(Activity)