SoFunction
Updated on 2025-04-10

How to change the format of some text in editText control

When we use the editText control, we encounter this problem. When I enter, when I choose to make the text thicker, the text I enter will become thicker. When I remove the selection and enter the text, the text is normal.

This situation is generally considered very simple. Isn't there a setTypeface method in editText? Just use edit_temp.setTypeface(()); But the problem is here. This method is to change the format of all text in editText. But the format I want is like this:Normal format with thicker format

public class FragmentAddNote extends Fragment implements OnClickListener { 
 //Define input text control private EditText edit_temp; 
 //Define the menu bar below the screen - the font becomes thicker button private LinearLayout linearLayout_Bold; 
 private ImageView img_Bold; 
 @Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
   Bundle savedInstanceState) { 
  View view = (.main_addnote, container, false); 
  initView(view);  
  return view; 
 } 
 public void initView(View view) 
 {    
  //Initialize the menu bar below the screen - Font thickening button  linearLayout_Bold = (LinearLayout)(.linearLayout_Bold); 
  linearLayout_Bold.setOnClickListener(this); 
  img_Bold = (ImageView)(.img_Bold); 
  //Initialize the input text control  edit_temp = (EditText)(.edit_temp); 
  edit_temp.addTextChangedListener(new editTextChangedListener()); 
 } 
 class editTextChangedListener implements TextWatcher{ 
  //Define the number of characters currently entered  private int CharCount = 0; 
  //s: All characters after the change  public void afterTextChanged(Editable s) {    
   //Move the cursor point to the last word   edit_temp.setSelection(()); 
  } 
  //s: All characters before the change; start: the position where the character starts; count: the total number of bytes before the change; after: the number of bytes after the change  public void beforeTextChanged(CharSequence s, int start, int count,int after) { 
  } 
  //S: All characters after change; start: the starting position of the character; before: the total number of bytes before change; count: the number of bytes after change;  public void onTextChanged(CharSequence s, int start, int before, int count) { 
   //Discern whether the number of characters currently entered is the same as the number of characters in the text box. If the number of characters is the same, no operation will be performed.   //It is mainly used to break out of the loop. When changing the text, onTextChanged believes that there is a change and will enter the dead loop, so this way ends the loop   if(CharCount!=edit_temp.length()) 
   {   
    //Give the length of the current string to the input string variable    CharCount = edit_temp.length();     
    //Define SpannableString, its main purpose is to change the format of some text in editText and TextView, as well as insert pictures into them, etc.    SpannableString ss = new SpannableString(s);     
    if(linearLayout_Bold.getTag().toString().equals("1")) 
    {      
     (new StyleSpan(Typeface.BOLD_ITALIC), start, (), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
     edit_temp.setText(ss); 
    } 
   }   
  }   
 } 
 @Override 
 public void onClick(View v) { 
  switch (()) { 
  case .linearLayout_Bold: 
   if(linearLayout_Bold.getTag().toString().equals("0")) 
   { 
    img_Bold.setImageResource(.ic_editor_bar_rtf_bold_on); 
    linearLayout_Bold.setTag("1"); 
    //edit_temp.setTypeface(()); 
   }else if(linearLayout_Bold.getTag().toString().equals("1")) 
   { 
    img_Bold.setImageResource(.ic_editor_bar_rtf_bold); 
    linearLayout_Bold.setTag("0");    
    //edit_temp.setTypeface(()); 
   } 
   break; 
  default: 
   break; 
  } 
 } 
} 

The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!