使用textview中的链接直接实现activity的跳转。

来源:互联网 发布:golang append 编辑:程序博客网 时间:2024/05/16 08:27

</pre>建立一个类直接继承activiry就好了,再用鼠标右键 source 添加实现方法。<p></p><p></p><pre name="code" class="java">package com.example.android_textview;import android.app.Activity;import android.os.Bundle;public class Activity1 extends Activity {public Activity1() {// TODO Auto-generated constructor stub}@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setTitle("Activity1");}}



可以用setTitle 直接来设置标题。


然后将activity直接添加进manifest就好了。

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.android_textview"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="17"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".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>    </application></manifest>


然后使用intent实现跳转(借助于mainifest中的配置 Activirty.class)。

package com.example.android_textview;import java.lang.reflect.Field;import android.R.string;import android.app.Activity;import android.content.Intent;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.text.Html;import android.text.Html.ImageGetter;import android.text.SpannableString;import android.text.Spanned;import android.text.method.LinkMovementMethod;import android.text.style.ClickableSpan;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.TextView;public class MainActivity extends Activity {public int getResourceId(String name) {try {// 根据资源的ID的变量名获得Field的对象,使用反射机制来实现的Field field = R.drawable.class.getField(name);// 取得并返回资源的id的字段(静态变量)的值,使用反射机制return Integer.parseInt(field.get(null).toString());} catch (Exception e) {// TODO: handle exception}return 0;}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);setTitle("用链接调用activity");TextView t1 = (TextView) findViewById(R.id.textView1);String s1 = "显示activity";SpannableString spannableString1 = new SpannableString(s1);spannableString1.setSpan(new ClickableSpan() {@Overridepublic void onClick(View widget) {// TODO Auto-generated method stubIntent intent = new Intent(MainActivity.this, Activity1.class);startActivity(intent);}}, 0, s1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);t1.setText(spannableString1);t1.setMovementMethod(LinkMovementMethod.getInstance());}@Overridepublic 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;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}



思路就是:

将text转化为spannableString,然后将这个spannableString直接添加在textivew的内容之中。(看代码就一目了然了)  然后中间的spannableString是有监听的。





0 0
原创粉丝点击