android_常用UI控件_01_TextView3_点击打开新的activity
点击textview打开新的activity
在网站设计制作、成都网站设计中从网站色彩、结构布局、栏目设置、关键词群组等细微处着手,突出企业的产品/服务/品牌,帮助企业锁定精准用户,提高在线咨询和转化,使成都网站营销成为有效果、有回报的无锡营销推广。创新互联建站专业成都网站建设10多年了,客户满意度97.8%,欢迎成都创新互联客户联系。
(1) MainActivity.java
package com.example.android_textview_opennewactivity; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.text.SpannableString; import android.text.Spanned; import android.text.method.LinkMovementMethod; import android.text.method.MovementMethod; import android.text.style.ClickableSpan; import android.view.Menu; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private TextView textview1; private TextView textview2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textview1 = (TextView) this.findViewById(R.id.textview01); textview2 = (TextView) this.findViewById(R.id.textview02); String text1,text2; text1 = "打开activity1"; text2 = "打开activity2"; //拆分字符串 SpannableString spannablestring1 = new SpannableString(text1); SpannableString spannablestring2 = new SpannableString(text2); spannablestring1.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { // TODO Auto-generated method stub //点击后打开新的意图 Intent intent = new Intent(MainActivity.this, Activity1.class); startActivity(intent); } }, 0, text1.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE); textview1.setText(spannablestring1); spannablestring2.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this, Activity2.class); startActivity(intent); } }, 0, text2.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE); textview2.setText(spannablestring2); //设置textview点击事件 textview1.setMovementMethod(LinkMovementMethod.getInstance()); textview2.setMovementMethod(LinkMovementMethod.getInstance()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
(2)activity_main.xml
(3)Activity1.java
package com.example.android_textview_opennewactivity; import android.app.Activity; import android.os.Bundle; 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); this.setTitle("Activity1"); } }
(4)Activity2.java
package com.example.android_textview_opennewactivity; import android.app.Activity; import android.os.Bundle; public class Activity2 extends Activity { public Activity2() { // TODO Auto-generated constructor stub } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setTitle("Activity2"); } }
(5)AndroidManifest.xml
网页名称:android_常用UI控件_01_TextView3_点击打开新的activity
文章链接:http://pwwzsj.com/article/iehsjh.html