android_常用UI控件_01_TextView3_点击打开新的activity

点击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>


android_常用UI控件_01_TextView3_点击打开新的activity,布布扣,bubuko.com

android_常用UI控件_01_TextView3_点击打开新的activity

上一篇:Android实现SQLite数据库的增、删、改、查的操作


下一篇:c++中内存拷贝函数(C++ memcpy)详解