在 textview 上链接文本中的域无效,链接不可点击
linkify on textview with domain in text not working, links not clickable
我无法 linkfy
带有 href
的字符串,因为该字符串表示带有它的域名。所以字符串。让我告诉你。
这是字符串:
<string name="go_to_settings">Please go to "Settings" in <a href="https://www.mywebsite.com/settings">mywebsite.com</a> to change this status</string>
它会告诉用户:"Please go to "Settings" in mywebsite.com 来改变这个状态。"
这是我正在使用的textview
:
<TextView
android:id="@+id/tv_instructions"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_marginTop="73dp"
android:lineSpacingExtra="3sp"
android:textAlignment="center"
android:textColor="#5c5c5c"
android:textColorLink="@color/action_blue"
android:linksClickable="true"
android:text="@string/go_to_settings"/>
这就是我尝试制作 link clickable
:
的方法
Linkify.addLinks(tv_instructions, Linkify.ALL)
tv_instructions.movementMethod=LinkMovementMethod.getInstance()
但我在 api 24 和 27 设备和模拟器上看到 link 将转到 www.mywebsite.com 而不是 www.mywebsite/settings
但奇怪的是,如果我更改用户看到的文本并从中删除 .com,那么它就可以正常工作。所以如果用户看到这个,它就会起作用:
它会告诉用户:"Please go to "设置“在我的网站中更改此状态。”
注意没有提到 .com。我怎样才能显示这个。我也试过这种方式:
tv_instructions.text=(utils.fromHTML(resources.getString(R.string.go_to_settings),null))
tv_instructions.movementMethod=LinkMovementMethod.getInstance()
和这样的文本视图:
<TextView
android:id="@+id/tv_instructions"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_marginTop="73dp"
android:lineSpacingExtra="3sp"
android:textAlignment="center"
android:textColor="#5c5c5c"
android:text="@string/go_to_settings"
/>
你们知道我做错了什么吗?
linkify on textview with domain in text not working, links not clickable
您也可以使用ClickableSpan
If an object of this type is attached to the text of a TextView
with a movement method of LinkMovementMethod
, the affected spans of text can be selected. If selected and clicked, the onClick(View)
method will be called.
检查下面的示例代码
试试这个
public class MainActivity extends AppCompatActivity {
TextView myTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTextView = (TextView) findViewById(R.id.myTextview);
setSpan();
}
private void setSpan() {
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("Please go to \"Settings\" in");
SpannableString spannableString = new SpannableString(" mywebsite.com ");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
Toast.makeText(MainActivity.this, "Click", Toast.LENGTH_SHORT).show();
// you can open your link here as per your requiremnt
}
};
spannableString.setSpan(clickableSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new UnderlineSpan(), 0, spannableString.length(), 0);
spannableStringBuilder.append(spannableString);
spannableStringBuilder.append("to change this status");
myTextView.setText(spannableStringBuilder);
myTextView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
想通了。出于某种原因,我需要在我的 strings
文件中使用标签 cData
将我的文本包装为字符数据。这有效:
<string name="go_to_settings"><![CDATA[Please go to "Settings" in <a href="https://www.mywebsite.com/settings">mywebsite.com</a> to change this status :]]></string>
然后 textview
看起来像这样:
<TextView
android:id="@+id/tv_instructions"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_marginTop="73dp"
android:lineSpacingExtra="3sp"
android:text="@string/go_to_settings"
android:textAlignment="center"
android:textColor="#5c5c5c"
android:textColorLink="@color/action_blue"/>
Java代码
tv_instructions.text=(utils.fromHTML(resources.getString(R.string.go_to_settings),null))
tv_instructions.movementMethod=LinkMovementMethod.getInstance()
我无法 linkfy
带有 href
的字符串,因为该字符串表示带有它的域名。所以字符串。让我告诉你。
这是字符串:
<string name="go_to_settings">Please go to "Settings" in <a href="https://www.mywebsite.com/settings">mywebsite.com</a> to change this status</string>
它会告诉用户:"Please go to "Settings" in mywebsite.com 来改变这个状态。"
这是我正在使用的textview
:
<TextView
android:id="@+id/tv_instructions"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_marginTop="73dp"
android:lineSpacingExtra="3sp"
android:textAlignment="center"
android:textColor="#5c5c5c"
android:textColorLink="@color/action_blue"
android:linksClickable="true"
android:text="@string/go_to_settings"/>
这就是我尝试制作 link clickable
:
Linkify.addLinks(tv_instructions, Linkify.ALL)
tv_instructions.movementMethod=LinkMovementMethod.getInstance()
但我在 api 24 和 27 设备和模拟器上看到 link 将转到 www.mywebsite.com 而不是 www.mywebsite/settings
但奇怪的是,如果我更改用户看到的文本并从中删除 .com,那么它就可以正常工作。所以如果用户看到这个,它就会起作用:
它会告诉用户:"Please go to "设置“在我的网站中更改此状态。”
注意没有提到 .com。我怎样才能显示这个。我也试过这种方式:
tv_instructions.text=(utils.fromHTML(resources.getString(R.string.go_to_settings),null))
tv_instructions.movementMethod=LinkMovementMethod.getInstance()
和这样的文本视图:
<TextView
android:id="@+id/tv_instructions"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_marginTop="73dp"
android:lineSpacingExtra="3sp"
android:textAlignment="center"
android:textColor="#5c5c5c"
android:text="@string/go_to_settings"
/>
你们知道我做错了什么吗?
linkify on textview with domain in text not working, links not clickable
您也可以使用ClickableSpan
If an object of this type is attached to the text of a
TextView
with a movement method ofLinkMovementMethod
, the affected spans of text can be selected. If selected and clicked, theonClick(View)
method will be called.
检查下面的示例代码
试试这个
public class MainActivity extends AppCompatActivity {
TextView myTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTextView = (TextView) findViewById(R.id.myTextview);
setSpan();
}
private void setSpan() {
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("Please go to \"Settings\" in");
SpannableString spannableString = new SpannableString(" mywebsite.com ");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
Toast.makeText(MainActivity.this, "Click", Toast.LENGTH_SHORT).show();
// you can open your link here as per your requiremnt
}
};
spannableString.setSpan(clickableSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new UnderlineSpan(), 0, spannableString.length(), 0);
spannableStringBuilder.append(spannableString);
spannableStringBuilder.append("to change this status");
myTextView.setText(spannableStringBuilder);
myTextView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
想通了。出于某种原因,我需要在我的 strings
文件中使用标签 cData
将我的文本包装为字符数据。这有效:
<string name="go_to_settings"><![CDATA[Please go to "Settings" in <a href="https://www.mywebsite.com/settings">mywebsite.com</a> to change this status :]]></string>
然后 textview
看起来像这样:
<TextView
android:id="@+id/tv_instructions"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_marginTop="73dp"
android:lineSpacingExtra="3sp"
android:text="@string/go_to_settings"
android:textAlignment="center"
android:textColor="#5c5c5c"
android:textColorLink="@color/action_blue"/>
Java代码
tv_instructions.text=(utils.fromHTML(resources.getString(R.string.go_to_settings),null))
tv_instructions.movementMethod=LinkMovementMethod.getInstance()