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

发布时间:2020-08-05 13:59:35 作者:xiaohongyangok
来源:网络 阅读:308

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


推荐阅读:
  1. andriod添加新的控件以及Activity相应的界面
  2. android_常用UI控件_02_EditText_01添加图片到edittext中

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android import public

上一篇:Python3爬虫利器Gerapy模块的安装方法是什么

下一篇:Python3爬虫是如何发送请求的

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》