SpannableString 显示富文本,设置点击事件时,长按就会报错
package permission.zhoujian.com.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private String mFirstBook = "《中国历史》";
private String mSecondBook = "《世界历史》";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView mTvContent = (TextView) findViewById(R.id.tv_content);
SpannableString mFirstBookSpan = new SpannableString(mFirstBook);
SpannableString mSecondBookSpan = new SpannableString(mSecondBook);
ClickableSpan mFirstBookClickSpan = new MyClickableSpan(mFirstBook);
ClickableSpan mSecondBookClickSpan = new MyClickableSpan(mSecondBook);
mFirstBookSpan.setSpan(mFirstBookClickSpan, 0, mFirstBook.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mSecondBookSpan.setSpan(mSecondBookClickSpan, 0, mSecondBook.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTvContent.append(mFirstBookSpan);
mTvContent.append("和");
mTvContent.append(mSecondBookSpan);
mTvContent.setHighlightColor(getResources().getColor(android.R.color.transparent));
mTvContent.setMovementMethod(LinkMovementMethod.getInstance());
}
private class MyClickableSpan extends ClickableSpan {
String text;
public MyClickableSpan(String text) {
super();
this.text = text;
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setARGB(255, 0, 0, 0);
ds.setColor(MainActivity.this.getResources().getColor(R.color.air_mode_txt_color));
ds.clearShadowLayer();
}
@Override
public void onClick(View widget) {
if (text.contains(mFirstBook)) {
Toast.makeText(MainActivity.this, "中国历史", Toast.LENGTH_SHORT).show();
} else if (text.contains(mSecondBook)) {
Toast.makeText(MainActivity.this, "世界历史", Toast.LENGTH_SHORT).show();
}
}
}
}
解决方案:屏蔽长按事件
mTvContent.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
return true;
}
});
完整代码:
package permission.zhoujian.com.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private String mFirstBook = "《中国历史》";
private String mSecondBook = "《世界历史》";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView mTvContent = (TextView) findViewById(R.id.tv_content);
mTvContent.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
return true;
}
});
SpannableString mFirstBookSpan = new SpannableString(mFirstBook);
SpannableString mSecondBookSpan = new SpannableString(mSecondBook);
ClickableSpan mFirstBookClickSpan = new MyClickableSpan(mFirstBook);
ClickableSpan mSecondBookClickSpan = new MyClickableSpan(mSecondBook);
mFirstBookSpan.setSpan(mFirstBookClickSpan, 0, mFirstBook.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mSecondBookSpan.setSpan(mSecondBookClickSpan, 0, mSecondBook.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTvContent.append(mFirstBookSpan);
mTvContent.append("和");
mTvContent.append(mSecondBookSpan);
mTvContent.setHighlightColor(getResources().getColor(android.R.color.transparent));
mTvContent.setMovementMethod(LinkMovementMethod.getInstance());
}
private class MyClickableSpan extends ClickableSpan {
String text;
public MyClickableSpan(String text) {
super();
this.text = text;
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setARGB(255, 0, 0, 0);
ds.setColor(MainActivity.this.getResources().getColor(R.color.air_mode_txt_color));
ds.clearShadowLayer();
}
@Override
public void onClick(View widget) {
if (text.contains(mFirstBook)) {
Toast.makeText(MainActivity.this, "中国历史", Toast.LENGTH_SHORT).show();
} else if (text.contains(mSecondBook)) {
Toast.makeText(MainActivity.this, "世界历史", Toast.LENGTH_SHORT).show();
}
}
}
}