類似MIUI原生短信編輯功能

首先描述下需求:

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ù)完善蓬网,或者有什么好的建議也可以給我提窒所。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市拳缠,隨后出現(xiàn)的幾起案子墩新,更是在濱河造成了極大的恐慌,老刑警劉巖窟坐,帶你破解...
    沈念sama閱讀 219,427評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件海渊,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡哲鸳,警方通過(guò)查閱死者的電腦和手機(jī)臣疑,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)徙菠,“玉大人讯沈,你說(shuō)我怎么就攤上這事⌒霰迹” “怎么了缺狠?”我有些...
    開封第一講書人閱讀 165,747評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)萍摊。 經(jīng)常有香客問我挤茄,道長(zhǎng),這世上最難降的妖魔是什么冰木? 我笑而不...
    開封第一講書人閱讀 58,939評(píng)論 1 295
  • 正文 為了忘掉前任穷劈,我火速辦了婚禮,結(jié)果婚禮上踊沸,老公的妹妹穿的比我還像新娘歇终。我一直安慰自己,他們只是感情好逼龟,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評(píng)論 6 392
  • 文/花漫 我一把揭開白布评凝。 她就那樣靜靜地躺著,像睡著了一般审轮。 火紅的嫁衣襯著肌膚如雪肥哎。 梳的紋絲不亂的頭發(fā)上辽俗,一...
    開封第一講書人閱讀 51,737評(píng)論 1 305
  • 那天疾渣,我揣著相機(jī)與錄音篡诽,去河邊找鬼。 笑死榴捡,一個(gè)胖子當(dāng)著我的面吹牛杈女,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播吊圾,決...
    沈念sama閱讀 40,448評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼达椰,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了项乒?” 一聲冷哼從身側(cè)響起啰劲,我...
    開封第一講書人閱讀 39,352評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎檀何,沒想到半個(gè)月后蝇裤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,834評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡频鉴,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評(píng)論 3 338
  • 正文 我和宋清朗相戀三年栓辜,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片垛孔。...
    茶點(diǎn)故事閱讀 40,133評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡藕甩,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出周荐,到底是詐尸還是另有隱情狭莱,我是刑警寧澤,帶...
    沈念sama閱讀 35,815評(píng)論 5 346
  • 正文 年R本政府宣布概作,位于F島的核電站腋妙,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏仆嗦。R本人自食惡果不足惜辉阶,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望瘩扼。 院中可真熱鬧谆甜,春花似錦、人聲如沸集绰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)栽燕。三九已至罕袋,卻和暖如春改淑,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背浴讯。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工朵夏, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人榆纽。 一個(gè)月前我還...
    沈念sama閱讀 48,398評(píng)論 3 373
  • 正文 我出身青樓仰猖,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親奈籽。 傳聞我的和親對(duì)象是個(gè)殘疾皇子饥侵,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容