From 429416a8fa88a9d3648afaae404666632b675a9c Mon Sep 17 00:00:00 2001 From: feiyanke Date: Thu, 12 Apr 2018 14:41:27 +0800 Subject: [PATCH] add TextView validate() extension --- api/current.txt | 5 ++ .../java/androidx/core/widget/TextViewTest.kt | 49 +++++++++++++++++++ .../java/androidx/core/widget/TextView.kt | 27 ++++++++++ 3 files changed, 81 insertions(+) create mode 100644 src/androidTest/java/androidx/core/widget/TextViewTest.kt create mode 100644 src/main/java/androidx/core/widget/TextView.kt diff --git a/api/current.txt b/api/current.txt index 4c56ac98..74e7dcda 100644 --- a/api/current.txt +++ b/api/current.txt @@ -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 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"); 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..6eab8f73 --- /dev/null +++ b/src/androidTest/java/androidx/core/widget/TextViewTest.kt @@ -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) + } +} \ No newline at end of file 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..5ed13e33 --- /dev/null +++ b/src/main/java/androidx/core/widget/TextView.kt @@ -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) +} \ No newline at end of file