【问题标题】:Android Edittext Rich Text FormattingAndroid Edittext 富文本格式
【发布时间】:2013-11-04 11:42:58
【问题描述】:

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

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

【问题讨论】:

    标签: android android-edittext rich-text-editor


    【解决方案1】:

    这里有两个很棒的库帮助了我。它们都是所见即所得的实现。

    Android 实时编辑器: https://github.com/1gravity/Android-RTEditor

    RichEditor-Android: https://github.com/wasabeef/richeditor-android

    还提供了它们的实现代码。

    【讨论】:

    • 感谢图书馆,我以后会用到它们的:)。
    • 我正在寻找相同的查询,但此页面中提供的答案并不令人满意。所以我想我会写一个。
    • 您目前的回答更实际,因为大约 2 年前,当我问这个问题时,还没有任何所见即所得的编辑器库。
    • 我使用富编辑器库并得到异常未捕获错误:NOT_FOUND_ERR: DOM Exception 8 at file:///android_asset/rich_editor.js 有人遇到同样的问题吗?
    【解决方案2】:

    没有这方面的库,但您可以使用以下类来做到这一点

    1.HTML

    2.可扩展

    3.ForegroundSpan

    4.BackgroundSpan

    5.AbsoluteSpan

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

    使用这个你可以直接嵌入带有 android 的 html 标签,比如粗体,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/

    【讨论】:

    • 感谢您的信息。还有一个问题 - 我可以使用哪个类来实现选定的文本对齐到左、右、中心?
    • 你可以使用 HTML 类,你会找到方法 HTML.fromHtml("here you can directly embed you html code of left rigth text") 并将其设置为 spannableString 并将 spannableSrig 设置为您的 textView完成。+1 接受
    • @Hardik,我尝试在编辑文本中使用该 html -bold-,但似乎没有变化?为什么会这样?
    • @gumuruh 感谢您的反馈,我已经为您编辑了一个答案,您可以查看官方示例以更好地了解如何操作。
    • 官方示例链接现在转到基本的入门指南。
    【解决方案3】:

    以下是将 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
        }
    
    }
    

    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) 在 Activity 中,加载编辑文本并创建 RichTextEditor 实例,如下所示

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

    【讨论】:

    • 得到异常未捕获的错误:NOT_FOUND_ERR: DOM Exception 8 at file:///android_asset/rich_editor.js你能告诉我有什么问题吗
    • @Karan sharma 看起来您使用的是不同的建议 (.js) 文件,并且您的 rich_editor.js 文件中似乎存在语法错误
    • 实际上库最初工作正常,现在我在 logcat 中遇到了这个异常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多