Main. xml is as follows:
[Html]
<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">
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerHorizontal = "true"
Android: layout_marginTop = "20dip"
Android: textSize = "18sp"
Android: text = "test: inserting images into EditText"/>
<EditText
Android: id = "@ + id/editText"
Android: layout_width = "fill_parent"
Android: layout_marginLeft = "30dip"
Android: layout_marginRight = "30dip"
Android: layout_height = "180dip"
Android: layout_marginTop = "50dip"
Android: hint = "insert emoticon"
/>
<Button
Android: id = "@ + id/button"
Android: layout_width = "fill_parent"
Android: layout_marginLeft = "30dip"
Android: layout_marginRight = "30dip"
Android: layout_height = "80dip"
Android: layout_marginTop = "250dip"
Android: hint = "insert emoticon"
/>
</RelativeLayout>
<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">
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerHorizontal = "true"
Android: layout_marginTop = "20dip"
Android: textSize = "18sp"
Android: text = "test: inserting images into EditText"/>
<EditText
Android: id = "@ + id/editText"
Android: layout_width = "fill_parent"
Android: layout_marginLeft = "30dip"
Android: layout_marginRight = "30dip"
Android: layout_height = "180dip"
Android: layout_marginTop = "50dip"
Android: hint = "insert emoticon"
/>
<Button
Android: id = "@ + id/button"
Android: layout_width = "fill_parent"
Android: layout_marginLeft = "30dip"
Android: layout_marginRight = "30dip"
Android: layout_height = "80dip"
Android: layout_marginTop = "250dip"
Android: hint = "insert emoticon"
/>
</RelativeLayout>
MainActivity is as follows:
[Java]
Package com.cn;
Import android. OS. Bundle;
Import android. text. SpannableString;
Import android. text. style. ImageSpan;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Import android. app. Activity;
Import android. graphics. BitmapFactory;
/**
* Demo description:
* Insert an image into EditText
*/
Public class MainActivity extends Activity {
Private EditText mEditText;
Private Button mButton;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Init ();
}
Private void init (){
MEditText = (EditText) findViewById (R. id. editText );
MButton = (Button) findViewById (R. id. button );
MButton. setOnClickListener (new ClickListenerImpl ());
}
Private class ClickListenerImpl implements OnClickListener {
@ Override
Public void onClick (View v ){
ImageSpan imageSpan = new ImageSpan (MainActivity. this,
BitmapFactory. decodeResource (getResources (), R. drawable. ic_launcher ));
SpannableString spannableString = new SpannableString ("test ");
SpannableString. setSpan (imageSpan, 0, spannableString. length (), SpannableString. SPAN_MARK_MARK );
MEditText. append (spannableString );
}
}
}
Package com.cn;
Import android. OS. Bundle;
Import android. text. SpannableString;
Import android. text. style. ImageSpan;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Import android. app. Activity;
Import android. graphics. BitmapFactory;
/**
* Demo description:
* Insert an image into EditText
*/
Public class MainActivity extends Activity {
Private EditText mEditText;
Private Button mButton;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Init ();
}
Private void init (){
MEditText = (EditText) findViewById (R. id. editText );
MButton = (Button) findViewById (R. id. button );
MButton. setOnClickListener (new ClickListenerImpl ());
}
Private class ClickListenerImpl implements OnClickListener {
@ Override
Public void onClick (View v ){
ImageSpan imageSpan = new ImageSpan (MainActivity. this,
BitmapFactory. decodeResource (getResources (), R. drawable. ic_launcher ));
SpannableString spannableString = new SpannableString ("test ");
SpannableString. setSpan (imageSpan, 0, spannableString. length (), SpannableString. SPAN_MARK_MARK );
MEditText. append (spannableString );
}
}
}