实例3:单击textView弹出activity
MainActivity的设置
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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 arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, Activity1.class);
startActivity(intent);
}
}, 0, text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString2.setSpan(new ClickableSpan() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.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());
}
}
下面是Activity1的设置(Activity2同)
//在创建Activity时,选择继承Activity
public class Activity1 extends Activity {
public Activity1() {
// TODO Auto-generated constructor stub
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setTitle("Activity1");
}
}
在布局文件里设置一下textView字体和边距属性即可。