-2

我想在我的活动中在我的图像下方设置超链接。例如,当我单击超链接时,新活动应该打开

我的 Main.java 文件

>package com.a.test;  
import android.app.Activity;  
import android.content.Intent;  
import android.net.Uri;  
import android.os.Bundle;  
import android.text.method.LinkMovementMethod;  
import android.view.View;  
import android.widget.Button;  
import android.widget.TextView;  
import android.widget.Toast;  
public class Main extends Activity {  
/** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
    }
}  

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="vertical" >

<ImageView
android:id="@+id/product"
android:layout_width="match_parent"
android:layout_height="192dp"
android:layout_gravity="top"
android:contentDescription="@string/test"
android:gravity="center"
android:scaleType="fitCenter"
android:src="@drawable/img9" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="  Brand Name: Dolce and Gabbana"
android:textColor="#000000" />

<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:text="  Color: Light Green"
android:textColor="#000000" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="  Product Name: Hand Bag"
android:textColor="#000000" />

<TextView
android:layout_width="310dp"
android:layout_height="wrap_content"
android:text="  Price: $599.89"
android:textColor="#000000" />
</LinearLayout> 
4

1 回答 1

0

设置应该包含以下链接的 TextView 的文本:

 SpannableString text = new SpannableString("Lorem ipsum dolor sit amet");
 ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(View view) {
        startActivity(new Intent(context , NewActivity.class));
    }
};
text.setSpan(clickableSpan, start, end, 0); 
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(text);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(text, BufferType.SPANNABLE);

请注意,“开始”和“结束”是您要作为链接的字符串部分的相应索引。

于 2012-08-14T19:14:21.920 回答