Skip to content
This repository was archived by the owner on Nov 14, 2018. It is now read-only.

Commit 68f0498

Browse files
committed
Add doOnEditorAction extension for TextView
1 parent 6a31b85 commit 68f0498

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

api/current.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,11 @@ package androidx.core.view {
653653

654654
package androidx.core.widget {
655655

656+
public final class TextViewKt {
657+
ctor public TextViewKt();
658+
method public static void doOnEditorAction(android.widget.TextView, int[] actionIds, kotlin.jvm.functions.Function1<? super android.view.KeyEvent,kotlin.Unit> action);
659+
}
660+
656661
public final class ToastKt {
657662
ctor public ToastKt();
658663
method public static android.widget.Toast toast(android.content.Context, CharSequence text, int duration = "Toast.LENGTH_SHORT");
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (C) 2018 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package androidx.core.widget
18+
19+
import android.support.test.InstrumentationRegistry
20+
import android.view.inputmethod.EditorInfo
21+
import android.widget.TextView
22+
import org.junit.Assert.assertEquals
23+
import org.junit.Test
24+
25+
class TextViewTest {
26+
private val textView = TextView(InstrumentationRegistry.getTargetContext())
27+
28+
@Test fun doOnEditorAction() {
29+
val actionDone = EditorInfo.IME_ACTION_DONE
30+
val actionNext = EditorInfo.IME_ACTION_NEXT
31+
val actionSend = EditorInfo.IME_ACTION_SEND
32+
textView.imeOptions = actionDone or actionNext
33+
34+
var calls = 0
35+
textView.doOnEditorAction(actionDone, actionNext) {
36+
calls++
37+
}
38+
39+
textView.onEditorAction(actionDone)
40+
assertEquals(calls, 1)
41+
textView.onEditorAction(actionNext)
42+
assertEquals(calls, 2)
43+
// as the IME_ACTION_SEND is not in the given actionIds, the listener shouldn't get called
44+
textView.onEditorAction(actionSend)
45+
assertEquals(calls, 2)
46+
}
47+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (C) 2018 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package androidx.core.widget
18+
19+
import android.view.KeyEvent
20+
import android.widget.TextView
21+
22+
/**
23+
* Performs the given [action] when an editor action with the given [actionId]
24+
* is performed on this [TextView]
25+
*/
26+
inline fun TextView.doOnEditorAction(
27+
vararg actionIds: Int,
28+
crossinline action: (event: KeyEvent?) -> Unit
29+
) {
30+
setOnEditorActionListener { _, id, event ->
31+
if (id in actionIds) {
32+
action(event)
33+
return@setOnEditorActionListener true
34+
}
35+
return@setOnEditorActionListener false
36+
}
37+
}

0 commit comments

Comments
 (0)