Android EditText加入图片混编显示

------------------------------------------------------------------------------gan------------------------------------------

//在文本插入图片发送到服务器

public void insetImage(String imageName,Bitmap image)
{
Editable eb = messageText.getEditableText();
//获得光标所在位置
int qqPosition = messageText.getSelectionStart();
SpannableString ss = new SpannableString( imageName);

//定义插入图片
Drawable drawable = new BitmapDrawable(image);
ss.setSpan(new ImageSpan(drawable,ImageSpan.ALIGN_BASELINE), 0 , ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
drawable.setBounds(2 , 0 , drawable.getIntrinsicWidth()+20, drawable.getIntrinsicHeight()+20);
//插入图片
eb.insert(qqPosition, ss);
}


----------------------------------------------------------------------------------------------

/**
* 接收到服务发送回来客户端的文本将其替换成图片
*/
public CharSequence addSmileySpans(CharSequence text)
{
SpannableStringBuilder builder = new SpannableStringBuilder(text);
Matcher matcher = mPattern.matcher(text);
while (matcher.find())
{
Bitmap icon = mSmileyToRes.get(matcher.group());
builder.setSpan(new ImageSpan(mContext, icon), matcher.start(),
matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return builder;
}


------------------------------------------------------------------------------gan------------------------------------------





public class MainActivity extends Activity {
private  EditText edit;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        edit = (EditText)findViewById(R.id.edit_text);
        Button button01 = (Button)findViewById(R.id.insert_text);
        button01.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Editable eb = edit.getEditableText();
//获得光标所在位置
int startPosition = edit.getSelectionStart();
//插入文本
eb.insert(startPosition, "www.onmoso.com");

}
});
       
        Button button02 = (Button)findViewById(R.id.insert_img);
        button02.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Editable eb = edit.getEditableText();
//获得光标所在位置
int startPosition = edit.getSelectionStart();
SpannableString ss = new SpannableString( "onmoso" );
//定义插入图片
Drawable drawable = getResources().getDrawable(R.drawable.icon);
ss.setSpan(new ImageSpan(drawable,ImageSpan.ALIGN_BASELINE), 0 , ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
drawable.setBounds(2 , 0 , drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
//插入图片
eb.insert(startPosition, ss);
}
});
    }


}






<?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        />
<EditText
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:hint="请输入内容"
   android:id="@+id/edit_text"
   />
<Button
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="插入文本 "
   android:id="@+id/insert_text"
   />
<Button
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="插入图片"
   android:id="@+id/insert_img"
   />
< /LinearLayout>
 
 
 
 
 
 
 
 
转自: http://blog.csdn.net/ganlijianstyle/article/details/7697047
 
 

你可能感兴趣的:(Android EditText加入图片混编显示)