我尝试使用此代码对字符串的一部分进行简单的点击,例如:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout salam = (LinearLayout) findViewById(R.id.salam);
TextView textView = new TextView(this);
textView.setHighlightColor(Color.RED);
String text = "Android is a Software stack";
SpannableString ss = new SpannableString(text);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
Toast.makeText(MainActivity.this, "asdasdasd", Toast.LENGTH_SHORT);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
};
ss.setSpan(clickableSpan, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ss);
salam.addView(textView);
}
但它不起作用,我无法点击文字来显示简单的Toast