我想在我的活动中在我的图片下方设置超链接。例如,当我点击超链接时,新活动应该打开
我的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);
}
}
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>
答案 0 :(得分: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);
请注意,“start”和“end”是您要作为链接创建的字符串部分的相应索引。