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 @@ -648,6 +648,11 @@ package androidx.core.view {

package androidx.core.widget {

public final class TextViewKt {
ctor public TextViewKt();
method public static void validate(android.widget.TextView, kotlin.jvm.functions.Function1<? super java.lang.CharSequence,java.lang.String> check);
}

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
49 changes: 49 additions & 0 deletions src/androidTest/java/androidx/core/widget/TextViewTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.widget.TextView
import org.junit.Assert.assertEquals
import org.junit.Test

class TextViewTest {
private val context = InstrumentationRegistry.getContext()
private val textView = TextView(context)

@Test
fun validateTest() {
textView.setText("1234")
textView.validate {
if (it.length > 2) {
"length>2"
} else {
null
}
}
assertEquals(textView.error, "length>2")
textView.setText("1")
textView.validate {
if (it.length > 2) {
"length>2"
} else {
null
}
}
assertEquals(textView.error, null)
}
}
27 changes: 27 additions & 0 deletions src/main/java/androidx/core/widget/TextView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.widget.TextView

/**
* check if text of TextView is valid and set error message by the return value of [check] lambda,
* if [check] return null means the text is valid
*/
fun TextView.validate(check: (CharSequence) -> String?) {
error = check(text)
}