textView跳转的activity

 
 

   点击textvie跳转到activity

    这里使用到SpannableString类,主要用于在指定的字符串点击触发的事件(具体实现代码如下)      在manifest.xml中添加两个activtity

  tv1.setMovementMethod(LinkMovementMethod.getInstance()); 添加链接

<activity android:name=".Activity1"></activity>
<activity android:name=".Activity2"></activity>

   


package com.android.textview1;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
                                                                                                                                                                                                                                               
    TextView tv1 = (TextView) this.findViewById(R.id.textview1);
    TextView tv2 = (TextView) this.findViewById(R.id.textview2);
    String text1 = "跳转到Activity1";
    String text2 = "跳转到Activity2";
    // spannableString用于拆分字符串
    SpannableString spannedString1 = new SpannableString(text1);
    SpannableString spannedString2 = new SpannableString(text2);
    spannedString1.setSpan(new ClickableSpan() {
        // 点击0到text.length触发事件
        @Override
        public void onClick(View widget) {
            // 创建一个界面跳转的意图
            Intent intent = new Intent(MainActivity.this, Activity1.class);
            // 启动意图
            startActivity(intent);
        }
                                                                                                                                                                                                                                                    
    }, 0, text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                                                                                                                                                                                                                                                
    spannedString2.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_EXCLUSIVE);
                                                                                                                                                                                                                                               
    tv1.setText(spannedString1);
    tv2.setText(spannedString2);
    // 设置连接
    tv1.setMovementMethod(LinkMovementMethod.getInstance());
    tv2.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;
    }
                                                                                                                                                                                                                                                
}


你可能感兴趣的:(Activity,textview,跳转,SpannableString)