代码实例:
01 | package com.onmoso.edittext.demo3; |
02 |
03 | import android.app.Activity; |
04 | import android.graphics.drawable.Drawable; |
05 | import android.os.Bundle; |
06 | import android.text.Editable; |
07 | import android.text.Spannable; |
08 | import android.text.SpannableString; |
09 | import android.text.style.ImageSpan; |
10 | import android.view.View; |
11 | import android.view.View.OnClickListener; |
12 | import android.widget.Button; |
13 | import android.widget.EditText; |
14 | /** |
15 | * |
16 | * @author xiangguang |
17 | * |
18 | */ |
19 | public class EditTextDemo3Activity extends Activity { |
20 | private EditText edit; |
21 | /** Called when the activity is first created. */ |
22 | @Override |
23 | public void onCreate(Bundle savedInstanceState) { |
24 | super .onCreate(savedInstanceState); |
25 | setContentView(R.layout.main); |
26 | |
27 | edit = (EditText)findViewById(R.id.edit_text); |
28 | Button button01 = (Button)findViewById(R.id.insert_text); |
29 | button01.setOnClickListener( new OnClickListener() { |
30 | |
31 | @Override |
32 | public void onClick(View v) { |
33 | // TODO Auto-generated method stub |
34 | Editable eb = edit.getEditableText(); |
35 | //获得光标所在位置 |
36 | int startPosition = edit.getSelectionStart(); |
37 | //插入文本 |
38 | eb.insert(startPosition, "www.onmoso.com" ); |
39 | |
40 | } |
41 | }); |
42 | |
43 | Button button02 = (Button)findViewById(R.id.insert_img); |
44 | button02.setOnClickListener( new OnClickListener() { |
45 | |
46 | @Override |
47 | public void onClick(View v) { |
48 | // TODO Auto-generated method stub |
49 | Editable eb = edit.getEditableText(); |
50 | //获得光标所在位置 |
51 | int startPosition = edit.getSelectionStart(); |
52 | SpannableString ss = new SpannableString( "onmoso" ); |
53 | //定义插入图片 |
54 | Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher); |
55 | ss.setSpan( new ImageSpan(drawable,ImageSpan.ALIGN_BASELINE), 0 , ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
56 | drawable.setBounds( 2 , 0 , drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); |
57 | //插入图片 |
58 | eb.insert(startPosition, ss); |
59 | } |
60 | }); |
61 | } |
62 | } |
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:layout_width = "fill_parent" |
04 | android:layout_height = "fill_parent" |
05 | android:orientation = "vertical" > |
06 |
07 | < TextView |
08 | android:layout_width = "fill_parent" |
09 | android:layout_height = "wrap_content" |
10 | android:text = "@string/hello" |
11 | /> |
12 | < EditText |
13 | android:layout_width = "fill_parent" |
14 | android:layout_height = "wrap_content" |
15 | android:hint = "请输入内容" |
16 | android:id = "@+id/edit_text" |
17 | /> |
18 | < Button |
19 | android:layout_width = "fill_parent" |
20 | android:layout_height = "wrap_content" |
21 | android:text = "插入文本 " |
22 | android:id = "@+id/insert_text" |
23 | /> |
24 | < Button |
25 | android:layout_width = "fill_parent" |
26 | android:layout_height = "wrap_content" |
27 | android:text = "插入图片" |
28 | android:id = "@+id/insert_img" |
29 | /> |
30 | </ LinearLayout > |
效果截图:
转载自:http://www.onmoso.com/Android/190.html