Skip to content

Commit

Permalink
Feature: added the field accessible and set cursor point color.
Browse files Browse the repository at this point in the history
  • Loading branch information
pokk committed Aug 30, 2018
1 parent c507f43 commit e97cd54
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ allprojects {
And add our dependency to your app `bundle.gradle`.

```gradle
implementation 'com.devrapid.jieyi:kotlinknifer:2.0.2'
implementation 'com.devrapid.jieyi:kotlinknifer:2.0.3'
implementation 'com.devrapid.jieyi:kotlinshaver:1.0.2'
implementation 'com.devrapid.jieyi:kotlinshaver:1.0.3'
```

Then you can use it!!!
Expand All @@ -101,14 +101,14 @@ Then you can use it!!!
<dependency>
<groupId>com.devrapid.jieyi</groupId>
<artifactId>kotlinknifer</artifactId>
<version>2.0.2</version>
<version>2.0.3</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.devrapid.jieyi</groupId>
<artifactId>kotlinshaver</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<type>pom</type>
</dependency>
```
Expand All @@ -129,14 +129,14 @@ Then you can use it!!!
If you'd not like to use them to your project, you can add the exclude as like below

```gradle
implementation('com.devrapid.jieyi:kotlinknifer:2.0.2', {
implementation('com.devrapid.jieyi:kotlinknifer:2.0.3', {
exclude group: 'com.google.code.gson', module: 'gson'
exclude group: 'com.github.bumptech.glide', module: 'glide'
})
```

```gradle
implementation('com.devrapid.jieyi:kotlinshaverr:0.2.2', {
implementation('com.devrapid.jieyi:kotlinshaverr:1.0.3', {
exclude group: 'io.reactivex.rxjava2', module: 'rxjava'
exclude group: 'io.reactivex.rxjava2', module: 'rxkotlin'
})
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ proj_name=kotlinknifer
proj_libname=KotlinKnifer
proj_group=com.devrapid.jieyi
proj_artifactid=kotlinknifer
proj_version=2.0.2
proj_version=2.0.3
proj_description=For developing an Android conveniently and rapidly.
proj_websiteurl=https://github.com/pokk/KotlinKnifer
proj_issuetrackerurl=https://github.com/pokk/KotlinKnifer/issues
Expand All @@ -39,4 +39,4 @@ [email protected]
proj_kotlin_name=kotlinshaver
proj_kotlin_libname=KotlinShaver
proj_kotlin_artifactid=kotlinshaver
proj_kotlin_version=1.0.2
proj_kotlin_version=1.0.3
35 changes: 35 additions & 0 deletions kotlinknifer/src/main/java/com/devrapid/kotlinknifer/EditText.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.devrapid.kotlinknifer

import android.graphics.PorterDuff.Mode.SRC_IN
import android.widget.EditText
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import com.devrapid.kotlinshaver.accessible

fun setCursorPointerColor(view: EditText, @ColorInt color: Int, @DrawableRes drawable: Int = -1) {
// Get the editor.
val editor = TextView::class.java.getDeclaredField("mEditor").accessible().get(view)
// Get all drawable's selection points.
val drawables = if (-1 == drawable)
listOf("mTextSelectHandleRes" // Selection cursor point.
, "mTextSelectHandleLeftRes" // Selection the highlight left cursor point.
, "mTextSelectHandleRightRes") // Selection the highlight right cursor point.
.asSequence()
.map { TextView::class.java.getDeclaredField(it).accessible() } // Get the drawables' field.
.map { it.getInt(view) } // Get the drawable resource id.
.map { requireNotNull(ContextCompat.getDrawable(view.context, it)) } // Get the drawable.
.map { it.setColorFilter(color, SRC_IN); it } // Change the color we set.
.toList()
else
(0..2).map { requireNotNull(ContextCompat.getDrawable(view.context, drawable)) }
// Get all handle fields.
val fields = listOf("mSelectHandleCenter"
, "mSelectHandleLeft"
, "mSelectHandleRight")
.map { editor.javaClass.getDeclaredField(it).accessible() }
fields.zip(drawables).forEach { (field, drawable) -> field.set(editor, drawable) }

view.highlightColor = color
}
7 changes: 7 additions & 0 deletions kotlinshaver/src/main/java/com/devrapid/kotlinshaver/Field.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@file:Suppress("NOTHING_TO_INLINE")

package com.devrapid.kotlinshaver

import java.lang.reflect.Field

inline fun Field.accessible() = apply { isAccessible = true }

0 comments on commit e97cd54

Please sign in to comment.