Skip to content
This repository was archived by the owner on Nov 14, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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<? super android.view.KeyEvent,kotlin.Unit> 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");
Expand Down
47 changes: 47 additions & 0 deletions src/androidTest/java/androidx/core/widget/TextViewTest.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
37 changes: 37 additions & 0 deletions src/main/java/androidx/core/widget/TextView.kt
Original file line number Diff line number Diff line change
@@ -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
}
}