情诗网 >表白情话 > 正文

支持表情,@mention,#话题#等功能的EditText控件

来源:情诗网    2020-12-02    分类:表白情话

输入@xxx #话题#等特殊字符,实现整体删除,文字高亮等功能的自定义EditText

项目地址 https://github.com/sunhapper/SpEditTool
思路介绍 自定义EditText轻松实现群聊精确@提及功能(@mention) 微博话题等功能
欢迎star,提PR、issue

ScreenShot

ScreenShot

emojiSp.gif

使用说明

Gradle

    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
dependencies {
            compile 'com.github.sunhapper:SpEditTool:0.2.3'
    }

xml

<com.sunhapper.spedittool.view.SpEditText
    android:id="@+id/spEdt"
    app:react_keys="#*%@"
    app:sp_mode="breakable"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

java

//参数为String,会被解析成char[]
spEditText.setReactKeys("@#%*");
spEditText.setKeyReactListener(new KeyReactListener() {
      @Override
      public void onKeyReact(String key) {
        //key被响应的字符,长度为1
        //todo 处理自己的逻辑
      }
    });
spEditText.insertSpecialStr(" @sunhapper ", true, 0, new ForegroundColorSpan(Color.RED));
  /**
   * 插入特殊字符串
   *
   * @param showContent 特殊字符串显示在文本框中的内容
   * @param rollBack 是否往前删除一个字符,因为@的时候可能留了一个字符在输入框里
   * @param customData 特殊字符串的数据结构
   * @param customSpan 特殊字符串的样式
   */
  public void insertSpecialStr(String showContent, boolean rollBack, Object customData,
      Object customSpan)
//获取到的数据是根据起点位置排过序的
SpData[] spDatas = spEditText.getSpDatas(); 
  public class SpData {

    /**
     * EditText中显示的内容
     */
    private String showContent;
    /**
     * 插入特殊字符串时传入的代表自定义数据结构的对象
     */
    private Object customData;
    /**
     * 特殊字符串的在完整字符串中的起点
     */
    private int start;
    /**
     * 特殊字符串的在完整字符串中的终点
     */
    private int end;
  }

彩蛋

GifTextUtil.setText(TextView textView,CharSequence text)
一行代码让TextView中ImageSpan支持Gif

热门文章