Android_ Common UI Controls _01_textview3_ click to open new activity

Source: Internet
Author: User

Click TextView to open a new 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 =  "Open Activity1";         text2 =  "Open Activity2";         //split string         SpannableString spannablestring1 = new  Spannablestring (Text1);         spannablestring spannablestring2  = new spannablestring (TEXT2);         Spannablestring1.setspan (New clickablespan ()  {              @Override      &nbsP;      public void onclick (View widget)  {                 // todo auto-generated  method stub                 //Click to open new intentions                  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);        &nbsP;textview2.settext (spannablestring2);         //settings TextView Click events          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" &NBsp;/>    <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} @Ove Rride protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub Super.oncre        Ate (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} @Ove Rride protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub Super.oncre        Ate (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= " />"     <application        android:allowbackup= " True "        android:icon=" @drawable/ic_launcher "         android:label= "@string/app_name"          Android:theme= "@style/apptheme"  >        <activity           &nbSp; 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>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.