下午好。您能帮我点击一下范围吗?事实是,当我在原始字符串的末尾分配可点击的子字符串时,不仅文本变得可点击,而且子字符串右边的整个TextView区域也变得可点击,这对我不利(有必要仅substring可以单击。您能给我一些建议吗?请为我的textview匹配父级。不幸的是,以编程方式在原始字符串中添加“”也是我不可接受的。Pastebin链接的代码为here。我在Dropbox上创建了一个视频:
我的程序工作不好:dropbox
我希望它像这样工作dropbox
我的TextView需要以下布局
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.text_view);
SpannableString spanable = new SpannableString("my clickable span");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "click span", Toast.LENGTH_SHORT).show();
}
};
spanable.setSpan(clickableSpan, 13, 17, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spanable);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
使用长度设置所有文本。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.text_view);
String strService = "my clickable span";
int serviceStart = strService.indexOf("my clickable span");
int serviceEnd = serviceStart + "my clickable span".length();
SpannableString spanable = new SpannableString(strService);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "click span", Toast.LENGTH_SHORT).show();
}
};
spanable.setSpan(clickableSpan, serviceStart, serviceEnd, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spanable);
textView.setClickable(true); //add clickable
textView.setMovementMethod(LinkMovementMethod.getInstance());
}