android 輸入表情icon

    1-輸入框 插入表情     

   Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.q);  
   ImageSpan imageSpan=new ImageSpan(this, bitmap);  
   SpannableString spannableString=new SpannableString("image"); 
   spannableString.setSpan(imageSpan, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
   //給輸入框追加表情
   editText.append(spannableString);  

     2- 給textview插入表情

  ImageSpan span = new ImageSpan(this, R.drawable.ic_launcher);
  SpannableString spanStr = new SpannableString("http://www.baidu.com");
  spanStr.setSpan(span, spanStr.length()-1, spanStr.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
  mTVText.setText(spanStr);

  3-修改textview局部文字顏色

  textView = (TextView) findViewById(R.id.textview);  
  SpannableStringBuilder builder = new SpannableStringBuilder(textView.getText().toString());  
  
  //ForegroundColorSpan 爲文字前景色,BackgroundColorSpan爲文字背景色  
  ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);  
  ForegroundColorSpan whiteSpan = new ForegroundColorSpan(Color.WHITE);  
  ForegroundColorSpan blueSpan = new ForegroundColorSpan(Color.BLUE);  
  ForegroundColorSpan greenSpan = new ForegroundColorSpan(Color.GREEN);  
  ForegroundColorSpan yellowSpan = new ForegroundColorSpan(Color.YELLOW);  

  builder.setSpan(redSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  builder.setSpan(whiteSpan, 1, 2, Spannable.SPAN_INCLUSIVE_INCLUSIVE);  
  builder.setSpan(blueSpan, 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  builder.setSpan(greenSpan, 3, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  builder.setSpan(yellowSpan, 4,5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  
  textView.setText(builder); 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章