您好,登錄后才能下訂單哦!
點擊textview打開新的activity
(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
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textview01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" /> <TextView android:id="@+id/textview02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:layout_marginTop="50dp" /> </RelativeLayout>
(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
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android_textview_opennewactivity" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.android_textview_opennewactivity.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Activity1" > </activity> <activity android:name=".Activity2" > </activity> </application> </manifest>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。