diff --git a/api/current.txt b/api/current.txt index 013f1b4d..9068a1c6 100644 --- a/api/current.txt +++ b/api/current.txt @@ -653,6 +653,11 @@ package androidx.core.view { package androidx.core.widget { + public final class TextViewKt { + ctor public TextViewKt(); + method public static void doOnEditorAction(android.widget.TextView, int[] actionIds, kotlin.jvm.functions.Function1 action); + } + public final class ToastKt { ctor public ToastKt(); method public static android.widget.Toast toast(android.content.Context, CharSequence text, int duration = "Toast.LENGTH_SHORT"); diff --git a/src/androidTest/java/androidx/core/widget/TextViewTest.kt b/src/androidTest/java/androidx/core/widget/TextViewTest.kt new file mode 100644 index 00000000..31f325b4 --- /dev/null +++ b/src/androidTest/java/androidx/core/widget/TextViewTest.kt @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package androidx.core.widget + +import android.support.test.InstrumentationRegistry +import android.view.inputmethod.EditorInfo +import android.widget.TextView +import org.junit.Assert.assertEquals +import org.junit.Test + +class TextViewTest { + private val textView = TextView(InstrumentationRegistry.getTargetContext()) + + @Test fun doOnEditorAction() { + val actionDone = EditorInfo.IME_ACTION_DONE + val actionNext = EditorInfo.IME_ACTION_NEXT + val actionSend = EditorInfo.IME_ACTION_SEND + textView.imeOptions = actionDone or actionNext + + var calls = 0 + textView.doOnEditorAction(actionDone, actionNext) { + calls++ + } + + textView.onEditorAction(actionDone) + assertEquals(calls, 1) + textView.onEditorAction(actionNext) + assertEquals(calls, 2) + // as the IME_ACTION_SEND is not in the given actionIds, the listener shouldn't get called + textView.onEditorAction(actionSend) + assertEquals(calls, 2) + } +} diff --git a/src/main/java/androidx/core/widget/TextView.kt b/src/main/java/androidx/core/widget/TextView.kt new file mode 100644 index 00000000..eb61d9f3 --- /dev/null +++ b/src/main/java/androidx/core/widget/TextView.kt @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package androidx.core.widget + +import android.view.KeyEvent +import android.widget.TextView + +/** + * Performs the given [action] when an editor action with the given [actionId] + * is performed on this [TextView] + */ +inline fun TextView.doOnEditorAction( + vararg actionIds: Int, + crossinline action: (event: KeyEvent?) -> Unit +) { + setOnEditorActionListener { _, id, event -> + if (id in actionIds) { + action(event) + return@setOnEditorActionListener true + } + return@setOnEditorActionListener false + } +}