运算符“+”不能应用于 android.text.SpannableString
Operator "+" cannot be aplied to android.text.SpannableString
enter code here
我正在创建一个小型 Android 应用程序。我想在文本视图中显示一个文本,其中包含多个要单击的部分。为此,我正在使用 android.text.SpannableString。这是我的代码:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Spannable sd = new SpannableString("Android Multiple clickable strings in textview");
final ClickableSpan clickableSpan3 = new ClickableSpan() {
@Override
public void onClick(View textView) {
Toast toast1 = Toast.makeText(getApplicationContext(), "first", Toast.LENGTH_SHORT);
toast1.setGravity(Gravity.CENTER, 0, 0);
toast1.show();
}
};
final ClickableSpan clickableSpan4 = new ClickableSpan() {
@Override
public void onClick(View textView) {
Toast toast2 = Toast.makeText(getApplicationContext(), "second", Toast.LENGTH_SHORT);
toast2.setGravity(Gravity.CENTER, 0, 0);
toast2.show();
}
};
sd.setSpan(clickableSpan3, 8, 16, SPAN_POINT_POINT);
sd.setSpan(clickableSpan4, 17, 26,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView textView = (TextView) findViewById(R.id.hello);
textView.setText(sd);
textView.setMovementMethod(new LinkMovementMethod());
}
它工作正常,但是如果我更改代码以将新词添加到 TextView
mTextView.setText(sd**+"my words"**);
mTextView.setMovementMethod(LinkMovementMethod.getInstance());
所有跨越的选择都消失了。如何避免?
使用TextUtils.concat(CharSequence... text)方法。
来自文档:
Returns a CharSequence concatenating the specified CharSequences,
retaining their spans if any.
代码应该是这样的:
mTextView.setText(TextUtils.concat(sd,"my words"));
enter code here
我正在创建一个小型 Android 应用程序。我想在文本视图中显示一个文本,其中包含多个要单击的部分。为此,我正在使用 android.text.SpannableString。这是我的代码:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Spannable sd = new SpannableString("Android Multiple clickable strings in textview");
final ClickableSpan clickableSpan3 = new ClickableSpan() {
@Override
public void onClick(View textView) {
Toast toast1 = Toast.makeText(getApplicationContext(), "first", Toast.LENGTH_SHORT);
toast1.setGravity(Gravity.CENTER, 0, 0);
toast1.show();
}
};
final ClickableSpan clickableSpan4 = new ClickableSpan() {
@Override
public void onClick(View textView) {
Toast toast2 = Toast.makeText(getApplicationContext(), "second", Toast.LENGTH_SHORT);
toast2.setGravity(Gravity.CENTER, 0, 0);
toast2.show();
}
};
sd.setSpan(clickableSpan3, 8, 16, SPAN_POINT_POINT);
sd.setSpan(clickableSpan4, 17, 26,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView textView = (TextView) findViewById(R.id.hello);
textView.setText(sd);
textView.setMovementMethod(new LinkMovementMethod());
}
它工作正常,但是如果我更改代码以将新词添加到 TextView
mTextView.setText(sd**+"my words"**); mTextView.setMovementMethod(LinkMovementMethod.getInstance());
所有跨越的选择都消失了。如何避免?
使用TextUtils.concat(CharSequence... text)方法。
来自文档:
Returns a CharSequence concatenating the specified CharSequences, retaining their spans if any.
代码应该是这样的:
mTextView.setText(TextUtils.concat(sd,"my words"));