以前一直不懂Spanned属性 SPAN_EXCLUSIVE_EXCLUSIVE, SPAN_EXCLUSIVE_EXCLUSIVE。
下面我以一个demo展示其意思
在 activity_main.xml activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我爱中国人" />
</RelativeLayout>
在Java代码
mTxtView = (EditText) findViewById(R.id.test);
SpannableString sp = new SpannableString(mTxtView.getText());
sp.setSpan(new ForegroundColorSpan(Color.RED), 1, 3,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mTxtView.setText(sp);
其结果是“我爱中国人” , 在EditText “爱”前、“中”后 插入文字时时 ,字不会显示红色
如果设置“SPAN_INCLUSIVE_INCLUSIVE”, 在“爱”前,“中”后插入文字时,字会变红色