kmn*_*kmn 140 android android-edittext
我需要一个EditText
看起来像这样的onError:
调用onError看起来像这样:
注意:该应用程序在SDK 19(4.4.2)上运行
min SDK是1
有没有类似于setError的方法自动执行此操作,还是我必须为其编写代码?
谢谢
reV*_*rse 310
有没有需要使用第三方库,因为谷歌推出TextInputLayout
的为部分design-support-library
.
遵循一个基本的例子:
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="true">
<android.support.design.widget.TextInputEditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name" />
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
注意:通过设置app:errorEnabled="true"
它的属性,TextInputLayout
一旦显示错误就不会改变它的大小 - 所以它基本上阻止了空间.
为了显示下面的错误的EditText
,你只需要调用#setError
的TextInputLayout
(不上孩子EditText
):
TextInputLayout til = (TextInputLayout) findViewById(R.id.text_input_layout);
til.setError("You need to enter a name");
Run Code Online (Sandbox Code Playgroud)
要隐藏错误并重置色调,只需调用即可til.setError(null)
.
为了使用TextInputLayout
您必须将以下内容添加到您的build.gradle
依赖项:
dependencies {
compile 'com.android.support:design:25.1.0'
}
Run Code Online (Sandbox Code Playgroud)
默认情况下,该行的行为EditText
红色.如果需要显示不同的颜色,可以在调用后立即使用以下代码setError
.
editText.getBackground().setColorFilter(getResources().getColor(R.color.red_500_primary), PorterDuff.Mode.SRC_ATOP);
Run Code Online (Sandbox Code Playgroud)
要清除它,只需调用该clearColorFilter
函数,如下所示:
editText.getBackground().clearColorFilter();
Run Code Online (Sandbox Code Playgroud)
你EditText
应该被包裹起来TextInputLayout
<android.support.design.widget.TextInputLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tilEmail">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/etEmail"
android:hint="Email"
android:layout_marginTop="10dp"
/>
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
要获得您想要的错误消息,请将错误设置为 TextInputLayout
TextInputLayout tilEmail = (TextInputLayout) findViewById(R.id.tilEmail);
if (error){
tilEmail.setError("Invalid email id");
}
Run Code Online (Sandbox Code Playgroud)
您应该添加设计支持库依赖项.在gradle依赖项中添加此行
compile 'com.android.support:design:22.2.0'
Run Code Online (Sandbox Code Playgroud)
呼叫myTextInputLayout.setError()
而不是myEditText.setError()
。
这些容器和容器在设置错误方面具有双重功能。您需要的功能是容器的功能。但是您可能需要最低版本的23。
reVerse 的答案很好,但它没有指出如何删除浮动错误工具提示之类的东西
你需要edittext.setError(null)
删除它。
另外,正如有人指出的那样,你不需要TextInputLayout.setErrorEnabled(true)
布局
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter something" />
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
代码
TextInputLayout til = (TextInputLayout) editText.getParent();
til.setError("Your input is not valid...");
editText.setError(null);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
110393 次 |
最近记录: |