2013-11-04 35 views
2

是否有任何庫或開放源代碼應用程序演示包含具有所見即所得界面的Rich Text Format Edittext組件。我聽說android-richtexteditor包含這樣的功能,但它的源代碼不再可用。Android編輯文本格式文本格式

如果有人有上述鏈接或來源,請與我分享。

回答

2

不存在這個沒有圖書館,但你可以做到這一點,使用下面的類

1.HTML

2.SPANNABLE

3.ForegroundSpan

4.BackgroundSpan

5.AbsoluteSpan

1. 01使用

這可以使嵌入直接HTML標籤與Android一樣大膽,itlic,underlince等

2. http://developer.android.com/reference/android/text/Spannable.html (SpannableString,SpannableStringBuilder等)

編輯

進行編輯文字粗體,斜體等。參見下面的一些示例鏈接

http://www.androidengineer.com/2010/08/easy-method-for-formatting-android.html

https://blog.stylingandroid.com/introduction-to-spans/

+0

感謝您的信息。還有一個問題 - 我可以使用哪個類來實現諸如選定的文本對齊到左側,右側,中間等事物? – Arshak92

+1

你可以使用HTML類,因爲你會發現方法HTML.fromHtml(「這裏你可以直接嵌入左邊rigth文本的html代碼」),並將其設置爲spannableString並將spannableStrig設置爲您的textView完成+1接受 – Hardik

+0

謝謝對於幫助 – Arshak92

0

下面是一個EditText轉換成富文本

1)創建一個使用RichTextEditor類的步驟,如下所示,本實施例具有用於支持粗體/取消粗體,可以根據需要添加更多的

import android.app.Activity; 
import android.graphics.Color; 
import android.graphics.drawable.Drawable; 
import android.text.Editable; 
import android.text.Selection; 
import android.text.Spannable; 
import android.text.TextWatcher; 
import android.text.style.StyleSpan; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.EditText; 
import android.widget.ImageButton; 

import com.loa.learnandcheck.R; 
import com.loa.learnandcheck.util.ResourceHelper; 

public class RichTextEditor implements ImageButton.OnClickListener, TextWatcher { 
    private boolean textBold; 
    private ImageButton buttonBold; 
    private EditText editText; 
    private Activity parent; 
    private int styleStart = 0; 

    public RichTextEditor(Activity parent, EditText editText){ 
     try { 
      this.parent = parent; 
      this.editText = editText; 
      init(); 
     }catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void init(){ 
     try { 
      buttonBold = (ImageButton)parent.findViewById(R.id.text_control_text_bold); 
      if(buttonBold!=null) { 
       buttonBold.setOnClickListener(this); 
      } 
      editText.addTextChangedListener(this); 
     }catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public Activity getParent() { 
     return parent; 
    } 

    public void setParent(Activity parent) { 
     this.parent = parent; 
    } 

    public void updateBackground(boolean itemSelected, ImageButton button) { 
     try { 
      if(itemSelected) { 
       button.setBackgroundColor(ResourceHelper.getThemeColor(parent,R.color.colorGray, Color.GRAY)); 
      } else { 
       button.setBackgroundColor(ResourceHelper.getThemeColor(parent,R.color.colorWhite, Color.WHITE)); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void handleBoldButtonClick() { 
     try { 
      textBold = !textBold; 
      updateBackground(textBold,buttonBold); 
      int selectionStart = editText.getSelectionStart(); 
      int selectionEnd = editText.getSelectionEnd(); 
      if (selectionStart > selectionEnd){ 
       int temp = selectionEnd; 
       selectionEnd = selectionStart; 
       selectionStart = temp; 
      } 
      if (selectionEnd > selectionStart) { 
       Spannable str = editText.getText(); 
       StyleSpan[] ss = str.getSpans(selectionStart, selectionEnd, StyleSpan.class); 
       boolean exists = false; 
       for (int i = 0; i < ss.length; i++) { 
        if (ss[i].getStyle() == android.graphics.Typeface.BOLD){ 
         str.removeSpan(ss[i]); 
         exists = true; 
        } 
       } 
       if (!exists){ 
        str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void handleFormat(Editable s, int position, int format) { 
     try { 
      StyleSpan[] ss = s.getSpans(styleStart, position, StyleSpan.class); 
      for (int i = 0; i < ss.length; i++) { 
       if (ss[i].getStyle() == format){ 
        s.removeSpan(ss[i]); 
       } 
      } 
      s.setSpan(new StyleSpan(format), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onClick(View view) { 
     try { 
      switch (view.getId()) { 
       case R.id.text_control_text_bold: 
        handleBoldButtonClick(); 
        break; 
       //more formats to be handled as needed here... 
       default: 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    public void afterTextChanged(Editable s) { 
     int position = Selection.getSelectionStart(editText.getText()); 
     //handle bold 
     if (textBold){ 
      handleFormat(s, position, android.graphics.Typeface.BOLD); 
     } 
     //more formats to be handled as needed here... 
    } 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
     styleStart = start; 
    } 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     //unused 
    } 

} 

2)創建以下ResourceHelper類

public class ResourceHelper { 
    /** 
    * Get a color value from a theme attribute. 
    * @param context used for getting the color. 
    * @param attribute theme attribute. 
    * @param defaultColor default to use. 
    * @return color value 
    */ 
    public static int getThemeColor(Context context, int attribute, int defaultColor) { 
     int themeColor = 0; 
     String packageName = context.getPackageName(); 
     try { 
      Context packageContext = context.createPackageContext(packageName, 0); 
      ApplicationInfo applicationInfo = 
       context.getPackageManager().getApplicationInfo(packageName, 0); 
      packageContext.setTheme(applicationInfo.theme); 
      Resources.Theme theme = packageContext.getTheme(); 
      TypedArray ta = theme.obtainStyledAttributes(new int[] {attribute}); 
      themeColor = ta.getColor(0, defaultColor); 
      ta.recycle(); 
     } catch (PackageManager.NameNotFoundException e) { 
      e.printStackTrace(); 
     } 
     return themeColor; 
    } 
} 

3)創建具有一個EditText和控制按鈕(ImageButtons佈局)如下所示

<EditText 
       android:id="@+id/text_content" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="5" 
       android:inputType="textMultiLine" 
       android:lines="5" 
       android:scrollbars="vertical" 
       android:background="@color/colorWhite" 

       android:hint="@string/text_content" /> 


<ImageButton 
          android:id="@+id/text_control_text_bold" 
          android:layout_width="40dp" 
          android:layout_height="40dp" 
          android:background="@color/colorWhite" 
          android:src="@drawable/ic_action_text_bold"/> 

4)在活動中,加載的EditText和創建使用RichTextEditor實例作爲顯示下面

inputText = (EditText)findViewById(R.id.text_content) ; 
new RichTextEditor(this,inputText);