首先描述下需求:
1宵凌、新增短信時(shí),進(jìn)來(lái)收件人是為空止后,顯示一行文字(提醒)
2瞎惫、從通訊錄選擇聯(lián)系人后,回到短信編輯界面译株,收件欄顯示一行瓜喇,內(nèi)容為“收件人:XXX、XXX歉糜、XXX欠橘、XXX.....”
3、當(dāng)點(diǎn)擊收件欄時(shí)现恼,收件欄內(nèi)容變化肃续,變成可刪除,最多顯示四行,多余四行有上下滑動(dòng)輪叉袍,不足四行始锚,是幾行顯示幾行
4、填寫短信內(nèi)容喳逛,即時(shí)計(jì)算短信條算瞧捌,并有文本提示
下面是實(shí)現(xiàn)后效果圖
下面終于到了代碼實(shí)現(xiàn)塊了,主要寫一些主要用到的類润文,其他不太重要的就忽略寫姐呐,讀者自行補(bǔ)全。
首先是短信收件人是一個(gè)自定義的流布局典蝌,這個(gè)流布局不是本人寫的曙砂,只是在上面加了些修改來(lái)滿足自己的業(yè)務(wù)需求。
TagFlowLayout類
[java]view plaincopy
importjava.util.HashSet;
importjava.util.Iterator;
importjava.util.Set;
importcom.zhaonongzi.wnshhseller.R;
importandroid.content.Context;
importandroid.content.res.TypedArray;
importandroid.graphics.Rect;
importandroid.os.Bundle;
importandroid.os.Parcelable;
importandroid.text.TextUtils;
importandroid.util.AttributeSet;
importandroid.util.Log;
importandroid.view.MotionEvent;
importandroid.view.View;
/**
*?自定義tag?頁(yè)面的整體頁(yè)面
*
*?實(shí)現(xiàn)綁定數(shù)據(jù)骏掀,事件處理
*?@author?admin
*
*/
publicclassTagFlowLayoutextendsFlowLayoutimplements
TagAdapter.OnDataChangedListener?{
privateTagAdapter?mTagAdapter;
privatebooleanmAutoSelectEffect?=true;
privateintmSelectedMax?=?-1;//?-1為不限制數(shù)量
privatestaticfinalString?TAG?="TagFlowLayout";
privateMotionEvent?mMotionEvent;
privateSet?mSelectedView?=newHashSet();
publicTagFlowLayout(Context?context,?AttributeSet?attrs,intdefStyle)?{
super(context,?attrs,?defStyle);
TypedArray?ta?=?context.obtainStyledAttributes(attrs,
R.styleable.TagFlowLayout);
mAutoSelectEffect?=?ta.getBoolean(R.styleable.TagFlowLayout_auto_select_effect,true);
mSelectedMax?=?ta.getInt(R.styleable.TagFlowLayout_max_select,?-1);
ta.recycle();
if(mAutoSelectEffect)?{
setClickable(true);
}
}
publicTagFlowLayout(Context?context,?AttributeSet?attrs)?{
this(context,?attrs,0);
}
publicTagFlowLayout(Context?context)?{
this(context,null);
}
@Override
protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec)?{
intcCount?=?getChildCount();
for(inti?=0;?i?<?cCount;?i++)?{
TagView?tagView?=?(TagView)?getChildAt(i);
if(tagView.getVisibility()?==?View.GONE)
continue;
if(tagView.getTagView().getVisibility()?==?View.GONE)?{
tagView.setVisibility(View.GONE);
}
}
super.onMeasure(widthMeasureSpec,?heightMeasureSpec);
}
publicinterfaceOnSelectListener?{
voidonSelected(Set?selectPosSet);
}
privateOnSelectListener?mOnSelectListener;
publicvoidsetOnSelectListener(OnSelectListener?onSelectListener)?{
mOnSelectListener?=?onSelectListener;
if(mOnSelectListener?!=null)
setClickable(true);
}
publicinterfaceOnTagClickListener?{
booleanonTagClick(View?view,intposition,?FlowLayout?parent);
}
privateOnTagClickListener?mOnTagClickListener;
publicvoidsetOnTagClickListener(OnTagClickListener?onTagClickListener)?{
mOnTagClickListener?=?onTagClickListener;
if(onTagClickListener?!=null)
setClickable(true);
}
publicvoidsetAdapter(TagAdapter?adapter)?{
//?if?(mTagAdapter?==?adapter)
//?return;
mTagAdapter?=?adapter;
mTagAdapter.setOnDataChangedListener(this);
changeAdapter();
}
privatevoidchangeAdapter()?{
removeAllViews();
TagAdapter?adapter?=?mTagAdapter;
TagView?tagViewContainer?=null;
HashSet?preCheckedList?=?mTagAdapter.getPreCheckedList();
for(inti?=0;?i?<?adapter.getCount();?i++)?{
View?tagView?=?adapter.getView(this,?i,?adapter.getItem(i));
tagViewContainer?=newTagView(getContext());
//?ViewGroup.MarginLayoutParams?clp?=?(ViewGroup.MarginLayoutParams)
//?tagView.getLayoutParams();
//?ViewGroup.MarginLayoutParams?lp?=?new
//?ViewGroup.MarginLayoutParams(clp);
//?lp.width?=?ViewGroup.LayoutParams.WRAP_CONTENT;
//?lp.height?=?ViewGroup.LayoutParams.WRAP_CONTENT;
//?lp.topMargin?=?clp.topMargin;
//?lp.bottomMargin?=?clp.bottomMargin;
//?lp.leftMargin?=?clp.leftMargin;
//?lp.rightMargin?=?clp.rightMargin;
tagView.setDuplicateParentStateEnabled(true);
tagViewContainer.setLayoutParams(tagView.getLayoutParams());
tagViewContainer.addView(tagView);
addView(tagViewContainer);
if(preCheckedList.contains(i))?{
tagViewContainer.setChecked(true);
}
}
mSelectedView.addAll(preCheckedList);
}
@Override
publicbooleanonTouchEvent(MotionEvent?event)?{
if(event.getAction()?==?MotionEvent.ACTION_UP)?{
mMotionEvent?=?MotionEvent.obtain(event);
}
returnsuper.onTouchEvent(event);
}
@Override
publicbooleanperformClick()?{
if(mMotionEvent?==null)
returnsuper.performClick();
intx?=?(int)?mMotionEvent.getX();
inty?=?(int)?mMotionEvent.getY();
mMotionEvent?=null;
TagView?child?=?findChild(x,?y);
intpos?=?findPosByView(child);
if(child?!=null)?{
doSelect(child,?pos);
if(mOnTagClickListener?!=null)?{
returnmOnTagClickListener.onTagClick(child.getTagView(),?pos,
this);
}
}
returnsuper.performClick();
}
publicvoidsetMaxSelectCount(intcount)?{
if(mSelectedView.size()?>?count)?{
Log.w(TAG,"you?has?already?select?more?than?"+?count
+"?views?,?so?it?will?be?clear?.");
mSelectedView.clear();
}
mSelectedMax?=?count;
}
publicSet?getSelectedList()?{
returnnewHashSet(mSelectedView);
}
privatevoiddoSelect(TagView?child,intposition)?{
if(mAutoSelectEffect)?{
if(!child.isChecked())?{
//?處理max_select=1的情況
if(mSelectedMax?==1&&?mSelectedView.size()?==1)?{
Iterator?iterator?=?mSelectedView.iterator();
Integer?preIndex?=?iterator.next();
TagView?pre?=?(TagView)?getChildAt(preIndex);
pre.setChecked(false);
child.setChecked(true);
mSelectedView.remove(preIndex);
mSelectedView.add(position);
}else{
if(mSelectedMax?>0
&&?mSelectedView.size()?>=?mSelectedMax)
return;
child.setChecked(true);
mSelectedView.add(position);
}
}else{
child.setChecked(false);
mSelectedView.remove(position);
}
if(mOnSelectListener?!=null)?{
mOnSelectListener
.onSelected(newHashSet(mSelectedView));
}
}
}
privatestaticfinalString?KEY_CHOOSE_POS?="key_choose_pos";
privatestaticfinalString?KEY_DEFAULT?="key_default";
@Override
protectedParcelable?onSaveInstanceState()?{
Bundle?bundle?=newBundle();
bundle.putParcelable(KEY_DEFAULT,super.onSaveInstanceState());
String?selectPos?="";
if(mSelectedView.size()?>0)?{
for(intkey?:?mSelectedView)?{
selectPos?+=?key?+"|";
}
selectPos?=?selectPos.substring(0,?selectPos.length()?-1);
}
bundle.putString(KEY_CHOOSE_POS,?selectPos);
returnbundle;
}
@Override
protectedvoidonRestoreInstanceState(Parcelable?state)?{
if(stateinstanceofBundle)?{
Bundle?bundle?=?(Bundle)?state;
String?mSelectPos?=?bundle.getString(KEY_CHOOSE_POS);
if(!TextUtils.isEmpty(mSelectPos))?{
String[]?split?=?mSelectPos.split("\\|");
for(String?pos?:?split)?{
intindex?=?Integer.parseInt(pos);
mSelectedView.add(index);
TagView?tagView?=?(TagView)?getChildAt(index);
tagView.setChecked(true);
}
}
super.onRestoreInstanceState(bundle.getParcelable(KEY_DEFAULT));
return;
}
super.onRestoreInstanceState(state);
}
privateintfindPosByView(View?child)?{
finalintcCount?=?getChildCount();
for(inti?=0;?i?<?cCount;?i++)?{
View?v?=?getChildAt(i);
if(v?==?child)
returni;
}
return-1;
}
privateTagView?findChild(intx,inty)?{
finalintcCount?=?getChildCount();
for(inti?=0;?i?<?cCount;?i++)?{
TagView?v?=?(TagView)?getChildAt(i);
if(v.getVisibility()?==?View.GONE)
continue;
Rect?outRect?=newRect();
v.getHitRect(outRect);
if(outRect.contains(x,?y))?{
returnv;
}
}
returnnull;
}
@Override
publicvoidonChanged()?{
changeAdapter();
}
}
FlowLayout類
[java]view plaincopy
importandroid.content.Context;
importandroid.util.AttributeSet;
importandroid.util.Log;
importandroid.view.View;
importandroid.view.ViewGroup;
importjava.util.ArrayList;
importjava.util.List;
importcom.zhaonongzi.wnshhseller.activity.customer.AddMessageActivity;
importcom.zhaonongzi.wnshhseller.utils.GlobalMemoryCache;
/**
*?Tag?頁(yè)面布局基類?主要實(shí)現(xiàn)測(cè)量子view,繪制view
*
*?@author?admin
*
*/
publicclassFlowLayoutextendsViewGroup?{
protectedList>?mAllViews?=newArrayList>();
protectedList?mLineHeight?=newArrayList();
privateintlines,?counts,?lastItem;//?限制顯示多少行
privatebooleanisShow;//?是否需要在最后顯示省略號(hào)
privateLastListerInterface?lastListerInterface?=null;
privateintihegth;
publicFlowLayout(Context?context,?AttributeSet?attrs,intdefStyle)?{
super(context,?attrs,?defStyle);
}
publicFlowLayout(Context?context,?AttributeSet?attrs)?{
this(context,?attrs,0);
}
publicFlowLayout(Context?context)?{
this(context,null);
}
publicvoidsetLines(intline)?{
this.lines?=?line;
}
publicintgetLastItem()?{
Log.e("lastItem",?lastItem?+"");
returnlastItem;
}
@Override
protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec)?{
intsizeWidth?=?MeasureSpec.getSize(widthMeasureSpec);
intmodeWidth?=?MeasureSpec.getMode(widthMeasureSpec);
intsizeHeight?=?MeasureSpec.getSize(heightMeasureSpec);
intmodeHeight?=?MeasureSpec.getMode(heightMeasureSpec);
//?wrap_content
intwidth?=0;
intheight?=0;
intlineWidth?=0;
intlineHeight?=0;
intchildHeight1?=0;
intcCount?=?getChildCount();
for(inti?=0;?i?<?cCount;?i++)?{
View?child?=?getChildAt(i);
if(child.getVisibility()?==?View.GONE)?{
if(i?==?cCount?-1)?{
width?=?Math.max(lineWidth,?width);
height?+=?lineHeight;
}
continue;
}
measureChild(child,?widthMeasureSpec,?heightMeasureSpec);
MarginLayoutParams?lp?=?(MarginLayoutParams)?child
.getLayoutParams();
intchildWidth?=?child.getMeasuredWidth()?+?lp.leftMargin
+?lp.rightMargin;
intchildHeight?=?child.getMeasuredHeight()?+?lp.topMargin
+?lp.bottomMargin;
childHeight1?=?childHeight;
if(lineWidth?+?childWidth?>?sizeWidth?-?getPaddingLeft()
-?getPaddingRight())?{
width?=?Math.max(width,?lineWidth);
lineWidth?=?childWidth;
height?+=?lineHeight;
lineHeight?=?childHeight;
}else{
lineWidth?+=?childWidth;
lineHeight?=?Math.max(lineHeight,?childHeight);
}
if(i?==?cCount?-1)?{
width?=?Math.max(lineWidth,?width);
height?+=?lineHeight;
}
if(lastItem?==0&&?height?==?(3*?childHeight))?{
lastItem?=?i?-1;
if(lastItem?>0&&null!=?lastListerInterface)?{
lastListerInterface.getLastItem(lastItem);
}
}
//?Log.e("lastItem",?lastItem?+?"");
}
height?=?(height?<=?(lines?*?childHeight1))???height
:?(lines?*?childHeight1);
ihegth?=?height;
AddMessageActivity.iheight?=?childHeight1;
setMeasuredDimension(
//
modeWidth?==?MeasureSpec.EXACTLY???sizeWidth?:?width
+?getPaddingLeft()?+?getPaddingRight(),
modeHeight?==?MeasureSpec.EXACTLY???sizeHeight?:?height
+?getPaddingTop()?+?getPaddingBottom()//
);
}
publicintgetHeigth()?{
returnihegth;
}
@Override
protectedvoidonLayout(booleanchanged,intl,intt,intr,intb)?{
mAllViews.clear();
mLineHeight.clear();
intwidth?=?getWidth();
intlineWidth?=0;
intlineHeight?=0;
List?lineViews?=newArrayList();
intcCount?=?getChildCount();
for(inti?=0;?i?<?cCount;?i++)?{
View?child?=?getChildAt(i);
if(child.getVisibility()?==?View.GONE)
continue;
MarginLayoutParams?lp?=?(MarginLayoutParams)?child
.getLayoutParams();
intchildWidth?=?child.getMeasuredWidth();
intchildHeight?=?child.getMeasuredHeight();
if(childWidth?+?lineWidth?+?lp.leftMargin?+?lp.rightMargin?>?width
-?getPaddingLeft()?-?getPaddingRight())?{
if(mAllViews.size()?<?lines)?{
mLineHeight.add(lineHeight);
mAllViews.add(lineViews);
lineWidth?=0;
lineHeight?=?childHeight?+?lp.topMargin?+?lp.bottomMargin;
lineViews?=newArrayList();
}
}
lineWidth?+=?childWidth?+?lp.leftMargin?+?lp.rightMargin;
lineHeight?=?Math.max(lineHeight,?childHeight?+?lp.topMargin
+?lp.bottomMargin);
lineViews.add(child);
}
mLineHeight.add(lineHeight);
mAllViews.add(lineViews);
intleft?=?getPaddingLeft();
inttop?=?getPaddingTop();
intlineNum?=?mAllViews.size();
AddMessageActivity.ilines?=?lineNum;
if(lines?>0)?{
lineNum?=?lineNum?>?lines???lines?:?lineNum;
}
for(inti?=0;?i?<?lineNum;?i++)?{
lineViews?=?mAllViews.get(i);
lineHeight?=?mLineHeight.get(i);
for(intj?=0;?j?<?lineViews.size();?j++)?{
View?child?=?lineViews.get(j);
if(child.getVisibility()?==?View.GONE)?{
continue;
}
MarginLayoutParams?lp?=?(MarginLayoutParams)?child
.getLayoutParams();
intlc?=?left?+?lp.leftMargin;
inttc?=?top?+?lp.topMargin;
intrc?=?lc?+?child.getMeasuredWidth();
intbc?=?tc?+?child.getMeasuredHeight();
child.layout(lc,?tc,?rc,?bc);
left?+=?child.getMeasuredWidth()?+?lp.leftMargin
+?lp.rightMargin;
}
left?=?getPaddingLeft();
top?+=?lineHeight;
}
if((boolean)?GlobalMemoryCache.getInstance().get("addMessage"))?{
AddMessageActivity.tfl_add_message_labellay
.setVisibility(View.GONE);
AddMessageActivity.scr_add_message_labellay
.setVisibility(View.GONE);
}
}
@Override
publicLayoutParams?generateLayoutParams(AttributeSet?attrs)?{
returnnewMarginLayoutParams(getContext(),?attrs);
}
@Override
protectedLayoutParams?generateDefaultLayoutParams()?{
returnnewMarginLayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
@Override
protectedLayoutParams?generateLayoutParams(LayoutParams?p)?{
returnnewMarginLayoutParams(p);
}
publicinterfaceLastListerInterface?{
publicvoidgetLastItem(intlastItem);
}
publicvoidsetLastListener(LastListerInterface?lastListerInterface)?{
this.lastListerInterface?=?lastListerInterface;
}
}
TagAdapter類
[java]view plaincopy
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.HashSet;
importjava.util.List;
importandroid.view.View;
/**
*?tag?布局適配器
*?@author?admin
*
*?@param?
*/
publicabstractclassTagAdapter
{
privateList?mTagDatas;
privateOnDataChangedListener?mOnDataChangedListener;
privateHashSet?mCheckedPosList?=newHashSet();
publicTagAdapter(List?datas)
{
mTagDatas?=?datas;
}
publicTagAdapter(T[]?datas)
{
mTagDatas?=newArrayList(Arrays.asList(datas));
}
staticinterfaceOnDataChangedListener
{
voidonChanged();
}
voidsetOnDataChangedListener(OnDataChangedListener?listener)
{
mOnDataChangedListener?=?listener;
}
publicvoidsetSelectedList(int...?pos)
{
for(inti?=0;?i?<?pos.length;?i++)
mCheckedPosList.add(pos[i]);
notifyDataChanged();
}
HashSet?getPreCheckedList()
{
returnmCheckedPosList;
}
publicintgetCount()
{
returnmTagDatas?==null?0:?mTagDatas.size();
}
publicvoidnotifyDataChanged()
{
mOnDataChangedListener.onChanged();
}
publicT?getItem(intposition)
{
returnmTagDatas.get(position);
}
publicabstractView?getView(FlowLayout?parent,intposition,?T?t);
}
TagView類
[java]view plaincopy
importandroid.content.Context;
importandroid.view.View;
importandroid.widget.Checkable;
importandroid.widget.FrameLayout;
/**
*?單個(gè)Tag?的自定義布局
*
*?@author?admin
*
*/
publicclassTagViewextendsFrameLayoutimplementsCheckable
{
privatebooleanisChecked;
privatestaticfinalint[]?CHECK_STATE?=newint[]{android.R.attr.state_checked};
publicTagView(Context?context)
{
super(context);
}
publicView?getTagView()
{
returngetChildAt(0);
}
@Override
publicint[]?onCreateDrawableState(intextraSpace)
{
int[]?states?=super.onCreateDrawableState(extraSpace?+1);
if(isChecked())
{
mergeDrawableStates(states,?CHECK_STATE);
}
returnstates;
}
/**
*?Change?the?checked?state?of?the?view
*
*?@param?checked?The?new?checked?state
*/
@Override
publicvoidsetChecked(booleanchecked)
{
if(this.isChecked?!=?checked)
{
this.isChecked?=?checked;
refreshDrawableState();
}
}
/**
*?@return?The?current?checked?state?of?the?view
*/
@Override
publicbooleanisChecked()
{
returnisChecked;
}
/**
*?Change?the?checked?state?of?the?view?to?the?inverse?of?its?current?state
*/
@Override
publicvoidtoggle()
{
setChecked(!isChecked);
}
}
上面是用到TagFlowLayout這個(gè)自定義控件需要的類鸠澈。
下面給出短信編輯的布局xml文件
[html]view plaincopy
xmlns:tag="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f5f5f5"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
android:id="@+id/message_mass_top"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"/>
android:id="@+id/linear_edit_message_add_receiver"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/message_mass_top"
android:orientation="horizontal">
android:id="@+id/txt_add_message_customer_declare"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:gravity="center"
android:paddingLeft="15dp"
android:text="收信人"
android:textColor="@color/black"
android:visibility="gone"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1">
android:id="@+id/txt_add_message_add_customer_btn"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left|center"
android:paddingLeft="15dp"
android:singleLine="true"
android:text="點(diǎn)擊添加收信客戶"
android:textColor="@color/gray"/>
android:id="@+id/scr_add_message_labellay"
android:layout_width="fill_parent"
android:layout_height="90dp"
android:visibility="gone">
android:id="@+id/tfl_add_message_labellay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ac_include"
android:layout_marginTop="4dp"
android:layout_weight="1"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:visibility="gone"
tag:max_select="-1"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:layout_gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp">
android:id="@+id/img_add_message_add_customer"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:src="@drawable/username"/>
android:id="@+id/txt_add_message_customer_counts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/black"
android:text="0"/>
android:id="@+id/line_1"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="@+id/linear_edit_message_add_receiver"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:background="#A0A0A0"/>
android:id="@+id/edittext_add_message_add_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/line_1"
android:background="@drawable/select_white_bg"
android:minHeight="80dp"
android:padding="15dp"
android:maxLength="650"
android:scrollbars="vertical"
android:maxHeight="200dp"
android:textColor="@color/black"/>
android:id="@+id/txt_add_message_add_content_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edittext_add_message_add_content"
android:layout_toLeftOf="@+id/txt_add_message_add_content_word"
android:paddingTop="10dp"
android:text="0"
android:textColor="@color/orange_title"/>
android:id="@+id/txt_add_message_add_content_word"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/edittext_add_message_add_content"
android:paddingRight="15dp"
android:paddingTop="10dp"
android:text=""
android:textColor="@color/gray"/>
實(shí)例化一個(gè)收件人adapter
[java]view plaincopy
adapter?=newTagAdapter(meBean.getReceiver())?{
finalLayoutInflater?mInflater?=?LayoutInflater
.from(AddMessageActivity.this);
@SuppressLint("NewApi")
@Override
publicView?getView(FlowLayout?parent,finalintposition,
finalString?s)?{
finalTextView?tv?=?(TextView)?mInflater.inflate(
R.layout.item_label_tv_medium,
tfl_add_message_labellay,false);
SpannableStringBuilder?sp1?=newSpannableStringBuilder(
s);//?姓名
SpannableStringBuilder?sp2?=newSpannableStringBuilder(
"x");//?刪除
sp1.setSpan(
newForegroundColorSpan(Color
.rgb(102,102,102)),0,?s.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
sp2.setSpan(
newForegroundColorSpan(Color.rgb(255,0,0)),
0,"X".length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
SpannableStringBuilder?sp?=newSpannableStringBuilder();//
sp.append(sp1).append(sp2);
if(position?==0)?{
tv.setText(s);
tv.setTextColor(android.graphics.Color
.parseColor("#666666"));
}else{
tv.setText(sp);
tv.setTextSize(16);
tv.setBackgroundResource(R.drawable.selector_contacts_gray_btn);
}
tv.setOnClickListener(newView.OnClickListener()?{
@Override
publicvoidonClick(View?v)?{
//?跳頁(yè)面帶值
if(position?!=0)?{
meBean.getReceiver().remove(position);
handler.sendEmptyMessage(1);
}
}
});
returntv;
}
};
計(jì)算短信條數(shù)方法:
[java]view plaincopy
/**
*?計(jì)算短信內(nèi)容條算
*
*?@param?wordNum
*?@return
*/
publicintgetSMSCounts(intwordNum)?{
intcounts;
if(wordNum?<=63)
counts?=1;
elseif(wordNum?<=127)
counts?=2;
else{
counts?=?(wordNum?-127)?%67==0??((wordNum?-127)?/67+2)
:?((wordNum?+7)?/67+1);
}
returncounts;
}
監(jiān)聽編輯文本框來(lái)提示短信條數(shù)和字?jǐn)?shù):
[java]view plaincopy
edittext_edit_message_add_content
.addTextChangedListener(newTextWatcher()?{
@Override
publicvoidonTextChanged(CharSequence?s,intstart,
intbefore,intcount)?{
//?TODO?Auto-generated?method?stub
}
@Override
publicvoidbeforeTextChanged(CharSequence?s,intstart,
intcount,intafter)?{
}
@Override
publicvoidafterTextChanged(Editable?s)?{
if(getSMSCounts(edittext_edit_message_add_content
.getText().toString().trim().length())?==1)?{
intc?=63-?edittext_edit_message_add_content
.getText().toString().trim().length();
txt_add_message_add_content_num.setText(c?+"");
txt_add_message_add_content_word.setText("");
}elseif(getSMSCounts(edittext_edit_message_add_content
.getText().toString().trim().length())?==2)?{
intc?=127-?edittext_edit_message_add_content
.getText().toString().trim().length();
txt_add_message_add_content_num.setText(c?+"");
txt_add_message_add_content_word.setText("(2)");
}elseif(getSMSCounts(edittext_edit_message_add_content
.getText().toString().trim().length())?>2)?{
intc?=?getSMSCounts(edittext_edit_message_add_content
.getText().toString().trim().length())
*67
-7
-?edittext_edit_message_add_content
.getText().toString().trim()
.length();
txt_add_message_add_content_num.setText(c?+"");
txt_add_message_add_content_word
.setText("("
+?getSMSCounts(edittext_edit_message_add_content
.getText().toString()
.trim().length())?+")");
}
}
});
接收刪除后的handler處理:
[java]view plaincopy
privateHandler?handler?=newHandler()?{
publicvoidhandleMessage(android.os.Message?msg)?{
if(msg.what?==1)?{
tfl_add_message_labellay.onChanged();
txt_add_message_customer_counts.setText((meBean.getReceiver()
.size()?-1)?+"");
}
};
};
上面已經(jīng)把關(guān)鍵的代碼都貼出來(lái)了柱告,其實(shí)最重要的 地方是按流布局去顯示收件人,原生的是沒有限制顯示行數(shù)笑陈,也沒有處理多行后的滑動(dòng)輪际度,頭疼的地方是去計(jì)算顯示高度并動(dòng)態(tài)設(shè)置scrollview的高地『祝坑都已經(jīng)踩過(guò)了乖菱,但是這個(gè)代碼的效率不是很高,有待繼續(xù)完善蓬网,或者有什么好的建議也可以給我提窒所。