"@you bang---
go on -------"
需要做分享内容,前面有段格式固定写死,同时颜色为灰色;后面的内容可以编辑,颜色为黑色,同时支持多行
有人用textview做固定格式,edittext做可编辑部分;
如果是单行,感觉还没多大问题,但多行,第二行起始位置就无法控制了。
1.字体部分颜色灰色问题:
对于android的显示字体样式,有Spannable对象,可以高亮部分字体,还可以给字体设置超链接
参考http://blog.csdn.net/hitlion2008/article/details/6856780
SpannableStringBuilder style=new SpannableStringBuilder(mShareTitle + shareContentString); style.setSpan(new ForegroundColorSpan(Color.LTGRAY),0,mShareTitle.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); shareContentTxt.setText(style); Editable b = shareContentTxt.getText(); shareContentTxt.setSelection(b.length());
2.@you不允许编辑问题:
偷换概念,发现如果edittext修改的对象在不许编辑区域,就让内容还原,从而产生不可编辑的错觉
这里用的TextWatcher
shareContentTxt.addTextChangedListener(new TextWatcher() { int current_start = -1; boolean last_changed = false; CharSequence current_s; @Override public void afterTextChanged(Editable s) { if (current_start < mShareTitle.length() && !last_changed) { last_changed = true; SpannableStringBuilder style=new SpannableStringBuilder(current_s); style.setSpan(new ForegroundColorSpan(Color.LTGRAY),0, mShareTitle.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); shareContentTxt.setText(style); shareContentTxt.setSelection(shareContentTxt.getText().length()); } else { last_changed = false; } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { current_start = start; current_s = s.toString(); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } });
他们将依次调用beforeTextChanged onTextChanged afterTextChanged方法,在这里修改editable内容需要有终止条件,否则很快就递归调用,栈溢出了。