android项目实战之编辑器集成

引言

项目需要用到编辑器,采用RichEditor,如下效果

实现

  1. 引入库2

    implementation 'jp.wasabeef:richeditor-android:2.0.0'

  2. XML

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_marginBottom="@dimen/dp_60" android:theme="@style/customTheme" >
    <HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/app_color_9b">

    复制代码
         <LinearLayout
             android:orientation="horizontal"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content">
    
             <ImageButton
                 android:id="@+id/action_undo"
                 android:layout_width="48dp"
                 android:layout_height="48dp"
                 android:background="@null"
                 android:contentDescription="@null"
                 android:src="@mipmap/undo" />
    
             <ImageButton
                 android:id="@+id/action_redo"
                 android:layout_width="48dp"
                 android:layout_height="48dp"
                 android:background="@null"
                 android:contentDescription="@null"
                 android:src="@mipmap/redo" />
    
             <ImageButton
                 android:id="@+id/action_bold"
                 android:layout_width="48dp"
                 android:layout_height="48dp"
                 android:background="@null"
                 android:contentDescription="@null"
                 android:src="@mipmap/bold" />
    
             <ImageButton
                 android:id="@+id/action_heading1"
                 android:layout_width="48dp"
                 android:layout_height="48dp"
                 android:background="@null"
                 android:contentDescription="@null"
                 android:src="@mipmap/h1" />
    
             <ImageButton
                 android:id="@+id/action_heading2"
                 android:layout_width="48dp"
                 android:layout_height="48dp"
                 android:background="@null"
                 android:contentDescription="@null"
                 android:src="@mipmap/h2" />
    
             <ImageButton
                 android:id="@+id/action_heading3"
                 android:layout_width="48dp"
                 android:layout_height="48dp"
                 android:background="@null"
                 android:contentDescription="@null"
                 android:src="@mipmap/h3" />
    
             <ImageButton
                 android:id="@+id/action_heading4"
                 android:layout_width="48dp"
                 android:layout_height="48dp"
                 android:background="@null"
                 android:contentDescription="@null"
                 android:src="@mipmap/h4" />
    
             <ImageButton
                 android:id="@+id/action_heading5"
                 android:layout_width="48dp"
                 android:layout_height="48dp"
                 android:background="@null"
                 android:contentDescription="@null"
                 android:src="@mipmap/h5" />
    
             <ImageButton
                 android:id="@+id/action_heading6"
                 android:layout_width="48dp"
                 android:layout_height="48dp"
                 android:background="@null"
                 android:contentDescription="@null"
                 android:src="@mipmap/h6" />
    
             <ImageButton
                 android:id="@+id/action_insert_image"
                 android:layout_width="48dp"
                 android:layout_height="48dp"
                 android:background="@null"
                 android:contentDescription="@null"
                 android:src="@mipmap/insert_image" />
    
    
         </LinearLayout>
    
     </HorizontalScrollView>
     <androidx.core.widget.NestedScrollView
         android:layout_width="match_parent"
         android:layout_height="wrap_content">
         <jp.wasabeef.richeditor.RichEditor
             android:id="@+id/editor"
             android:layout_width="match_parent"
             android:layout_height="wrap_content" />
     </androidx.core.widget.NestedScrollView>
    
     <TextView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="20dp"
         android:text="详情预览"
         android:visibility="gone"
         android:textSize="12sp" />
    
     <TextView
         android:id="@+id/preview"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="15dp"
         android:visibility="gone"/>
    </LinearLayout>
  3. fragement片段初始化

    private void initEditor(){
    mPreview = (TextView) mActivity.findViewById(R.id.preview);
    mEditor = (RichEditor) mActivity.findViewById(R.id.editor);
    //初始化编辑高度
    mEditor.setEditorHeight(200);
    //初始化字体大小
    mEditor.setEditorFontSize(16);
    //初始化字体颜色
    mEditor.setEditorFontColor(Color.BLACK);
    //初始化内边距
    mEditor.setPadding(10, 10, 10, 10);
    //设置默认显示语句
    mEditor.setPlaceholder("请输入...");
    //设置编辑器是否可用
    mEditor.setInputEnabled(true);

    复制代码
         //mPreview = (TextView) mActivity.findViewById(R.id.preview);
    
         mEditor.setOnTextChangeListener(new RichEditor.OnTextChangeListener() {
             @Override
             public void onTextChange(String text) {
                 mPreview.setText(text);
             }
         });
    
         mActivity.findViewById(R.id.action_undo).setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 mEditor.undo();
             }
         });
    
         mActivity.findViewById(R.id.action_redo).setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 mEditor.redo();
             }
         });
    
         mActivity.findViewById(R.id.action_heading1).setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 mEditor.setHeading(1);
             }
         });
    
         mActivity.findViewById(R.id.action_heading2).setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 mEditor.setHeading(2);
             }
         });
    
         mActivity.findViewById(R.id.action_heading3).setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 mEditor.setHeading(3);
             }
         });
    
         mActivity.findViewById(R.id.action_heading4).setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 mEditor.setHeading(4);
             }
         });
    
         mActivity.findViewById(R.id.action_heading5).setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 mEditor.setHeading(5);
             }
         });
    
         mActivity.findViewById(R.id.action_heading6).setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 mEditor.setHeading(6);
             }
         });
相关推荐
添砖java‘’5 小时前
vim高效编辑:从入门到精通
linux·编辑器·操作系统·vim
非得登录才能看吗?10 小时前
VScode 入门(设置篇)
ide·vscode·编辑器
AlphaFinance13 小时前
Windows下Vscode连接到WSL的方法
ide·vscode·编辑器
空影星21 小时前
Tablecruncher,一款轻量级CSV编辑器
python·编辑器·电脑·智能硬件
开发游戏的老王1 天前
虚幻引擎入门教程:虚幻编辑器的基本操作
编辑器·游戏引擎·虚幻
止观止1 天前
如何开发 VSCode 内置扩展:从零开始构建最简扩展
ide·vscode·编辑器
楚韵天工1 天前
宠物服务平台(程序+文档)
java·网络·数据库·spring cloud·编辑器·intellij-idea·宠物
呱呱巨基1 天前
vim编辑器
linux·笔记·学习·编辑器·vim
ii_best2 天前
IOS/ 安卓开发工具按键精灵Sys.GetAppList 函数使用指南:轻松获取设备已安装 APP 列表
android·开发语言·ios·编辑器
热爱生活的五柒2 天前
vscode左边打开文件后会覆盖上一个打开的文件,有什么不覆盖的方法
ide·vscode·编辑器