作者的github地址:https://github.com/wasabeef/richeditor-android
坑1:图片过大问题
解决办法:
将alt内容修改为下列形式,即将图片按手机屏幕大小进行展示,如果觉得图片仍然过大,可将100%改小即可
坑2:无法自动换行
解决办法:
我们需要重写RichEditor,去继承它,然后去使用。期间我们会用到2个调用js的方法:
exec("javascript:RE.prepareInsert();"); exec("javascript:RE.insertHTML('<br></br>');")
这两个方法是我们调用Js里面的插入HTML代码的功能,写好之后直接在
onTextChangeListener里面进行调用即可。这样我们就能进行换行。
思考:其实作者提供了insertHTML这个js方法,我们后期可以添加其他自己想要的功能,这个需要对html有一点点了解。下面贴一下我的具体代码,大家参考一下,相互学习
public class CustomRichEditor extends RichEditor { public CustomRichEditor(Context context) { super(context); } public CustomRichEditor(Context context, AttributeSet attrs) { super(context, attrs); } public CustomRichEditor(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public void setNewLine() { exec("javascript:RE.prepareInsert();"); exec("javascript:RE.insertHTML('<br></br>');"); }
private boolean mHasAddImg = false;
etRichContent.setOnTextChangeListener(new RichEditor.OnTextChangeListener() { @Override public void onTextChange(String text) { if (mHasAddImg) { mHasAddImg = false; etRichContent.setNewLine(); } } });
mHasAddImg = true; etRichContent.insertImage(imgPath, "picvision\" style=\"max-width:100% ");