删除错误消息时,不会删除TextInputLayout的错误视图

时间:2015-11-06 14:24:08

标签: android android-design-library android-textinputlayout

我有一个带有一些输入字段的垂直线性布局。使用TextInputLayout我得到一个带有标签和内置错误消息的漂亮流程。我的问题是当我添加和删除错误消息时。

如果我添加错误消息,它会位于编辑文本下方,一切看起来都不错。

如果我使用setError(null)删除错误消息,则会删除该消息,但该空间仍然存在。这显然是每个谷歌设计(见https://code.google.com/p/android/issues/detail?id=176005)。我非常希望删除这个空间,因为它会让UI看起来非常错误。

如果我执行.setErrorEnabled(false),视图将被删除,一切看起来都正常。但是,如果用户更改数据并执行另一个setError,则不会显示错误消息(仅编辑文本行为红色)。

2 个答案:

答案 0 :(得分:22)

从支持库版本23.1.1开始(可能更早),不再是这种情况。您应该能够调用TextInputLayout.setErrorEnabled(false)隐藏错误TextView,如果错误不为空或空,则现在调用TextInputLayout.setError(error)内部调用TextInputLayout.setErrorEnabled(true)。请参阅下面的代码段,取自支持库:

public void setError(@Nullable CharSequence error) {
    if (!mErrorEnabled) {
        if (TextUtils.isEmpty(error)) {
            // If error isn't enabled, and the error is empty, just return
            return;
        }
        // Else, we'll assume that they want to enable the error functionality
        setErrorEnabled(true);
    }
    ...
}

答案 1 :(得分:0)

对我来说,下面的代码工作正常。

13:56:38,104 INFO  [org.jboss.as.repository] (DeploymentScanner-threads - 2) WFLYDR0001: Content added at location C:\Risk_Mgt\Downloads\wildfly-10.0.0.Final\standalone\data\content\76\46dbc0aa1700b71ef90ada1bc3c326e8d02776\content
13:56:38,109 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-1) WFLYJCA0019: Stopped Driver service with driver-name = SpringBootES.war_org.h2.Driver_1_4
13:56:38,119 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 249) WFLYUT0022: Unregistered web context: /SpringBootES
13:56:38,126 INFO  [io.undertow.servlet] (ServerService Thread Pool -- 249) Closing Spring root WebApplicationContext
13:56:38,127 INFO  [org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext] (ServerService Thread Pool -- 249) Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@42447031: startup date [Wed Mar 21 13:30:53 EDT 2018]; root of context hierarchy
13:56:38,129 INFO  [org.springframework.jmx.export.annotation.AnnotationMBeanExporter] (ServerService Thread Pool -- 249) Unregistering JMX-exposed beans on shutdown
13:56:40,019 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-3) WFLYSRV0028: Stopped deployment SpringBootES.war (runtime-name: SpringBootES.war) in 1913ms
13:56:40,023 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-5) WFLYSRV0027: Starting deployment of "SpringBootES.war" (runtime-name: "SpringBootES.war")
13:56:51,926 WARN  [org.jboss.as.ee] (MSC service thread 1-2) WFLYEE0007: Not installing optional component org.springframework.http.server.ServletServerHttpAsyncRequestControl due to an exception (enable DEBUG log level to see the cause)
13:56:51,928 WARN  [org.jboss.as.ee] (MSC service thread 1-2) WFLYEE0007: Not installing optional component org.springframework.web.context.request.async.StandardServletAsyncWebRequest due to an exception (enable DEBUG log level to see the cause)
13:56:52,006 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-2) WFLYJCA0004: Deploying JDBC-compliant driver class org.h2.Driver (version 1.4)
13:56:52,012 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-1) WFLYJCA0018: Started Driver service with driver-name = SpringBootES.war_org.h2.Driver_1_4
13:56:52,093 INFO  [io.undertow.servlet] (ServerService Thread Pool -- 260) 2 Spring WebApplicationInitializers detected on classpath
13:56:53,033 INFO  [stdout] (ServerService Thread Pool -- 260) 

13:56:53,033 INFO  [stdout] (ServerService Thread Pool -- 260)   .   ____          _            __ _ _

13:56:53,034 INFO  [stdout] (ServerService Thread Pool -- 260)  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \

13:56:53,034 INFO  [stdout] (ServerService Thread Pool -- 260) ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

13:56:53,034 INFO  [stdout] (ServerService Thread Pool -- 260)  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )

13:56:53,034 INFO  [stdout] (ServerService Thread Pool -- 260)   '  |____| .__|_| |_|_| |_\__, | / / / /

13:56:53,035 INFO  [stdout] (ServerService Thread Pool -- 260)  =========|_|==============|___/=/_/_/_/

13:56:53,055 INFO  [stdout] (ServerService Thread Pool -- 260)  :: Spring Boot ::        (v1.5.2.RELEASE)

13:56:53,056 INFO  [stdout] (ServerService Thread Pool -- 260) 

13:56:53,241 INFO  [com.aexp.prospect.atules.AtulApplication] (ServerService Thread Pool -- 260) Starting AtulApplication on NYKPC06712N with PID 16880 (started by pveerana in C:\Risk_Mgt\Downloads\wildfly-10.0.0.Final\bin)
13:56:53,241 INFO  [com.aexp.prospect.atules.AtulApplication] (ServerService Thread Pool -- 260) No active profile set, falling back to default profiles: default
13:56:53,304 INFO  [org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext] (ServerService Thread Pool -- 260) Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@61eb59d6: startup date [Wed Mar 21 13:56:53 EDT 2018]; root of context hierarchy
13:56:55,604 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (ServerService Thread Pool -- 260) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
13:56:55,877 INFO  [io.undertow.servlet] (ServerService Thread Pool -- 260) Initializing Spring embedded WebApplicationContext
13:56:55,878 INFO  [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 260) Root WebApplicationContext: initialization completed in 2573 ms
13:56:57,166 INFO  [org.springframework.boot.web.servlet.ServletRegistrationBean] (ServerService Thread Pool -- 260) Mapping servlet: 'dispatcherServlet' to [/]
13:56:57,168 INFO  [org.springframework.boot.web.servlet.FilterRegistrationBean] (ServerService Thread Pool -- 260) Mapping filter: 'errorPageFilter' to: [/*]
13:56:57,168 INFO  [org.springframework.boot.web.servlet.FilterRegistrationBean] (ServerService Thread Pool -- 260) Mapping filter: 'characterEncodingFilter' to: [/*]
13:56:57,168 INFO  [org.springframework.boot.web.servlet.FilterRegistrationBean] (ServerService Thread Pool -- 260) Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
13:56:57,168 INFO  [org.springframework.boot.web.servlet.FilterRegistrationBean] (ServerService Thread Pool -- 260) Mapping filter: 'httpPutFormContentFilter' to: [/*]
13:56:57,168 INFO  [org.springframework.boot.web.servlet.FilterRegistrationBean] (ServerService Thread Pool -- 260) Mapping filter: 'requestContextFilter' to: [/*]
13:56:58,042 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] (ServerService Thread Pool -- 260) Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@61eb59d6: startup date [Wed Mar 21 13:56:53 EDT 2018]; root of context hierarchy
13:56:58,226 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 260) Mapped "{[/api/eshost/{eshost}/indexName/{indexName}/_basicSearch],methods=[GET]}" onto public java.lang.String com.aexp.prospect.atules.controllers.AtulSearchController.basicSearch(java.lang.String,java.lang.String,java.lang.String,java.lang.String)
13:56:58,235 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 260) Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
13:56:58,235 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 260) Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
13:56:58,306 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (ServerService Thread Pool -- 260) Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
13:56:58,306 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (ServerService Thread Pool -- 260) Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
13:56:58,503 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (ServerService Thread Pool -- 260) Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
13:56:58,924 INFO  [org.springframework.jmx.export.annotation.AnnotationMBeanExporter] (ServerService Thread Pool -- 260) Registering beans for JMX exposure on startup
13:56:58,988 INFO  [com.aexp.prospect.atules.AtulApplication] (ServerService Thread Pool -- 260) Started AtulApplication in 6.67 seconds (JVM running for 50698.335)
13:56:58,994 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 260) Initializing Mojarra 2.2.12-jbossorg-2 20150729-1131 for context '/SpringBootES'
13:57:03,889 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 260) WFLYUT0021: Registered web context: /SpringBootES
13:57:03,975 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) WFLYSRV0016: Replaced deployment "SpringBootES.war" with deployment "SpringBootES.war"
13:57:03,976 INFO  [org.jboss.as.controller] (DeploymentScanner-threads - 2) WFLYCTL0183: Service status report
WFLYCTL0186:   Services which failed to start:      service jboss.undertow.deployment.default-server.default-host."/atules-0.0.1-SNAPSHOT": org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host."/atules-0.0.1-SNAPSHOT": java.lang.RuntimeException: javax.servlet.ServletException: Failed to instantiate WebApplicationInitializer class

13:57:03,986 INFO  [org.jboss.as.repository] (DeploymentScanner-threads - 2) WFLYDR0002: Content removed from location C:\Risk_Mgt\Downloads\wildfly-10.0.0.Final\standalone\data\content\e4\796fce1370fb5d7b11d99012c33f8a30730b2a\content