Giter Site home page Giter Site logo

mentionedittext's Introduction

MentionEditText

GitHub tag (latest by date)

一个可定制性优良支持@的输入框组件。支持定制响应字符,自定义高亮Span,输入与数据及时同步。

预览

preview.gif

开始使用

引入依赖

在module下的build.gradle文件中添加如下依赖

    implementation 'studio.kio:MentionLibrary:1.0.0'

使用MentionEditText控件

    <studio.kio.mentionlibrary.MentionEditText
        android:id="@+id/et_sample"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    val myMentionEditText = MentionEditText(context)

自定义一个可被关联的数据类

    data class User(
        val Id: Int,
        val name: String
    )

启用@功能

        MentionUtil.withType(User::class)//返回MentionHandlerBuilder以支持mention事件及定制
            .attach(et_sample)//返回MentionEditTextHandler以支持插入及获取已关联的mentionItems

定制

    mentionHandlerBuilder.tag('@')//设置响应的字符
    .onMention(object : OnMentionInsertedListener<User> {
         override fun onMentionInserted(position: Int) {
             //用户输入了@时这里会被回调
         }
     })
    .decorate(object : MentionHandlerBuilder.MentionDecorator {
        override fun getSpan(): Any {
            //自定义高亮样式
            return ForegroundColorSpan((0xff000000 or (Math.random() * Int.MAX_VALUE).toLong()).toInt())
        }
    })

插入

    mentionHandler.insert(user, label, position)

获取关联的MentionItem

    mentionHandler.getMentionItems()//List<MentionItem>

完整Demo

this.mentionHandler = MentionUtil.withType(User::class)
            .onMention(object : OnMentionInsertedListener<User> {
                override fun onMentionInserted(position: Int) {
                    mentionHandler.insert(user, user.name, position)
                }
            })
            .decorate(object : MentionHandlerBuilder.MentionDecorator {
                override fun getSpan(): Any {
                    return ForegroundColorSpan((0xff000000 or (Math.random() * Int.MAX_VALUE).toLong()).toInt())
                }
            })
            .tag('@')
            .attach(et_sample)

//触发@
mentionHandler.automaticallyAppend()

//获取关联的用户
mentionHandler.getMentionItems()

mentionedittext's People

Contributors

elip-kio avatar kio-eurmy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

loper7 findvoid

mentionedittext's Issues

不错,个人认为该库有以下缺陷

1.一次性插入多条逻辑存在问题(这个我自行处理了一下,需要的话我可提供pr)
2.没有数据恢复方法(应用场景:编辑完成之后保存到草稿箱,然后从草稿箱取值恢复编辑)
开源不易,加油~

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.