1,可以在布局文件中设置
<EditText
android:id="@+id/editText1"
android:textColor="#2BD54D"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</EditText>
2,在代码中显示html代码
editText2.setText(Html.fromHtml( "<font color=#E61A6B>红色代码</font> "+ "<i><font color=#1111EE>蓝色斜体代码</font></i>"+"<u><i><font color=#1111EE>蓝色斜体加粗体下划线代码</font></i></u>"));
效果图
package rw.textView;
import android.R.integer;
import android.app.Activity;
import android.app.SearchManager.OnCancelListener;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
public class TextViewTestActivity extends Activity {
/** Called when the activity is first created. */
private EditText editText,editText2,editText3;
private ImageButton imageButton01,imageButton02,imageButton03;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText=(EditText) findViewById(R.id.editText1);
editText2=(EditText) findViewById(R.id.editText2);
editText3=(EditText) findViewById(R.id.editText3);
imageButton01=(ImageButton) findViewById(R.id.imageButton1);
imageButton02=(ImageButton) findViewById(R.id.imageButton2);
imageButton03=(ImageButton) findViewById(R.id.imageButton3);
editText2.setText(Html.fromHtml( "<font color=#E61A6B>红色代码</font> "+ "<i><font color=#1111EE>蓝色斜体代码</font></i>"+"<u><i><font color=#1111EE>蓝色斜体加粗体下划线代码</font></i></u>"));
// editText3.setText(Html.fromHtml("<img src='"+R.drawable.qq+"'/>", imageGetter,null));
imageButton01.setOnClickListener(new MyListener());
imageButton02.setOnClickListener(new MyListener());
imageButton03.setOnClickListener(new MyListener());
}
class MyListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.imageButton1:
SetImage(R.drawable.amazed);
break;
case R.id.imageButton2:
SetImage(R.drawable.angry);
break;
case R.id.imageButton3:
SetImage(R.drawable.isync);
break;
default:
break;
}
}
}
void SetImage(int dra)
{
Drawable drawable=getResources().getDrawable(dra);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
SpannableString spannableString = new SpannableString("pics");
ImageSpan imageSpan=new ImageSpan(drawable,ImageSpan.ALIGN_BASELINE);
spannableString.setSpan(imageSpan, 0, spannableString.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText3.setText(spannableString);
}
}