我有一个视图显示textview并启用自动链接。
<TextView
android:id="@+id/txt_contents"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="30dp"
android:textColor="@color/txt_code_itemlist"
android:autoLink="web"
android:textSize="22sp" />
现在,当我从textview点击链接时,它将打开浏览器。但是当我点击相同的链接时,我想找到打开新活动的方法。
答案 0 :(得分:0)
完美的工作:
tvoublie.setPaintFlags(tvoublie.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
Linkify.addLinks(tvoublie, Linkify.ALL);
tvoublie.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("LoginActivity", "Sign Up Activity activated.");
// this is where you should start the new Activity
Intent intent = new Intent(CurrentActivity.this,YourNewActivity.class);
startActivity(intent);
}
});
这用url打开o浏览器:
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("https://www.google.com"));
startActivity(intent);
}
});
答案 1 :(得分:0)
您可以在TextView中使用Linkify,
TextView sample = (TextView) findViewById(R.id.txt_contents);
String webUrlOrText = "www.google.com a search Engine";
Spannable spannableString = new SpannableString(Html.fromHtml(webUrlOrText));
Linkify.addLinks(spannableString, Linkify.WEB_URLS); // It will make only Weblink clickable
URLSpan[] urlSpans = spannableString.getSpans(0, spannableString.length(), URLSpan.class);
for (URLSpan urlSpan : urlSpans) {
LinkSpan linkSpan = new LinkSpan(urlSpan.getURL());
int spanStart = spannableString.getSpanStart(urlSpan);
int spanEnd = spannableString.getSpanEnd(urlSpan);
spannableString.setSpan(linkSpan, spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.removeSpan(urlSpan);
}
sample.setText(spannableString);
sample.setMovementMethod(LinkMovementMethod.getInstance());
和LinkSpan类,
private class LinkSpan extends URLSpan {
private LinkSpan(String url) {
super(url);
}
@Override
public void onClick(View view) {
String url = getURL();
if (url != null) {
// Open the Activity here
Log.d(TAG, "Url clicked");
}
}
}
答案 2 :(得分:-1)
您可以添加此项。
text_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(CurrentActivity.this,YourNewActivity.class);
startActivity(intent);
}
});
希望这有帮助。
答案 3 :(得分:-1)
使用以下代码识别单击的文本部分
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ClassroomLog.log(TAG, "Textview Click listener ");
if (tv.getSelectionStart() == -1 && tv.getSelectionEnd() == -1) {
//This condition will satisfy only when it is not an autolinked text
//Fired only when you touch the part of the text that is not hyperlinked
}
}
});
并编写代码以使用任何一种情况执行任何操作