欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android如何改變editText控件中部分文字的格式

 更新時(shí)間:2017年03月06日 08:40:38   作者:ganchuanpu  
本文主要介紹了android改變editText控件中部分文字格式的方法,具有很好的參考價(jià)值。下面跟著小編一起來看下吧

我們在使用editText控件的時(shí)候,會(huì)遇到這樣的一問題,就是我在輸入時(shí)候,當(dāng)我選擇讓文字變粗時(shí),我輸入的文字就會(huì)變粗,當(dāng)我去掉選擇時(shí),再輸入文字時(shí),文字就是正常情況了。

這種情況,大家一般認(rèn)為很簡單啊。editText中不是有setTypeface這個(gè)方法嗎。只要使用edit_temp.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));就可以了??墒菃栴}來了。這種方法,是將editText中所有的文字的格式全變了??墒俏蚁胍母袷绞沁@樣的:  正常格式變粗的格式正常的格式

public class FragmentAddNote extends Fragment implements OnClickListener { 
 //定義輸入文本控件 
 private EditText edit_temp; 
 //定義屏幕下面菜單欄--字體變粗按鈕 
 private LinearLayout linearLayout_Bold; 
 private ImageView img_Bold; 
 @Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
   Bundle savedInstanceState) { 
  View view = inflater.inflate(R.layout.main_addnote, container, false); 
  initView(view);  
  return view; 
 } 
 public void initView(View view) 
 {    
  //初始化屏幕下面菜單欄--字體變粗按鈕 
  linearLayout_Bold = (LinearLayout)view.findViewById(R.id.linearLayout_Bold); 
  linearLayout_Bold.setOnClickListener(this); 
  img_Bold = (ImageView)view.findViewById(R.id.img_Bold); 
  //初始化輸入文本控件 
  edit_temp = (EditText)view.findViewById(R.id.edit_temp); 
  edit_temp.addTextChangedListener(new editTextChangedListener()); 
 } 
 class editTextChangedListener implements TextWatcher{ 
  //定義當(dāng)前輸入的字符數(shù) 
  private int CharCount = 0; 
  //s:變化后的所有字符 
  public void afterTextChanged(Editable s) {    
   //將光標(biāo)點(diǎn),移動(dòng)到最后一個(gè)字 
   edit_temp.setSelection(s.length()); 
  } 
  //s:變化前的所有字符; start:字符開始的位置; count:變化前的總字節(jié)數(shù);after:變化后的字節(jié)數(shù) 
  public void beforeTextChanged(CharSequence s, int start, int count,int after) { 
  } 
  //S:變化后的所有字符;start:字符起始的位置;before: 變化之前的總字節(jié)數(shù);count:變化后的字節(jié)數(shù) 
  public void onTextChanged(CharSequence s, int start, int before, int count) { 
   //判斷當(dāng)前輸入的字符數(shù),與文本框內(nèi)的字符數(shù)長度是否一樣,如果一樣,則不進(jìn)行操作 
   //主要用來跳出循環(huán),當(dāng)改變文字時(shí),onTextChanged就認(rèn)為有所變化,會(huì)進(jìn)入死循環(huán),所以采用這種方式結(jié)束循環(huán) 
   if(CharCount!=edit_temp.length()) 
   {   
    //將當(dāng)前字符串的長度給輸入字符串變量 
    CharCount = edit_temp.length();     
    //定義SpannableString,它主要的用途就是可以改變editText,TextView中部分文字的格式,以及向其中插入圖片等功能 
    SpannableString ss = new SpannableString(s);     
    if(linearLayout_Bold.getTag().toString().equals("1")) 
    {      
     ss.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), start, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
     edit_temp.setText(ss); 
    } 
   }   
  }   
 } 
 @Override 
 public void onClick(View v) { 
  switch (v.getId()) { 
  case R.id.linearLayout_Bold: 
   if(linearLayout_Bold.getTag().toString().equals("0")) 
   { 
    img_Bold.setImageResource(R.drawable.ic_editor_bar_rtf_bold_on); 
    linearLayout_Bold.setTag("1"); 
    //edit_temp.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); 
   }else if(linearLayout_Bold.getTag().toString().equals("1")) 
   { 
    img_Bold.setImageResource(R.drawable.ic_editor_bar_rtf_bold); 
    linearLayout_Bold.setTag("0");    
    //edit_temp.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL)); 
   } 
   break; 
  default: 
   break; 
  } 
 } 
} 

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

最新評論