安卓Edittext富文本格式化

8

有没有任何库或开源应用程序演示包含所见即所得界面的富文本格式EditText组件。我听说android-richtexteditor包含了这样的功能,但它的源代码已不再可用。

如果有人有以上链接或资源,请与我分享。

3个回答

12

谢谢提供这些库,我将来会用到它们的 :)。 - Arshak92
1
我正在寻找相同的查询,但是这个页面提供的答案并不令人满意。所以我想写一个答案。 - sg_dev
你的回答目前更准确,因为大约两年前我提出这个问题时还没有任何所见即所得编辑器库。 - Arshak92
我使用了富文本编辑器库,但是出现了异常 Uncaught Error: NOT_FOUND_ERR: DOM Exception 8 at file:///android_asset/rich_editor.js。有人遇到过同样的问题吗? - Karan sharma

3

目前没有相关的库,但你可以使用以下类来实现。

1.HTML

2.SPANNABLE

3.ForegroundSpan

4.BackgroundSpan

5.AbsoluteSpan

1.http://developer.android.com/reference/android/text/Html.html

使用这个功能,你可以在安卓中直接嵌入HTML标签,如加粗、斜体、下划线等。
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/


谢谢提供信息。还有一个问题——我可以使用哪个类来实现选定文本的左对齐、右对齐和居中等功能? - Arshak92
1
你可以使用HTML类,其中包含方法HTML.fromHtml("在这里直接嵌入左右文本的HTML代码"),将其设置为SpannableString,并将SpannableString设置为你的textView。完成。+1 接受。 - Hardik
@Hardik,我尝试在EditText中使用html -bold-,但似乎没有变化?为什么会这样呢? - gumuruh
@gumuruh,感谢您的反馈。我已经为您编辑了一个答案,请查看官方示例以更好地了解如何实现它。 - Hardik
官方示例链接现在已经指向了一个基础的入门指南。 - Michael Fulton
@MichaelFulton 我修改了链接...谢谢 - Hardik

1
以下是将EditText转换为RichText的步骤:
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
    }

}

创建以下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) 创建布局,其中包含编辑文本框和控制按钮(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) 在Activity中加载edittext并创建RichTextEditor实例,如下所示

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

出现异常 Uncaught Error: NOT_FOUND_ERR: DOM Exception 8 at file:///android_asset/rich_editor.js 你能告诉我出了什么问题吗? - Karan sharma
@Karan Sharma 看起来你正在使用一个不同的建议(.js)文件,而且你的rich_editor.js文件似乎有语法错误。 - Bruce
实际上,库最初工作得很好,现在我在logcat中得到了这个异常。 - Karan sharma
它出现了错误: 无法获取提供者androidx.core.content.FileProvider:java.lang.SecurityException:提供程序必须授予URI权限。 - Abdullah Programmer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接