如何将 Spinner 设为“禁用”?状态看起来已禁用?
当我禁用 Spinner 时,它看起来几乎与禁用之前一模一样,即
之前
之后
它已被禁用,因此功能上一切都很好,但是我想要它看起来已禁用。这个问题似乎是围绕陷阱提出的(此处 和此处< /strong> 例如)但是最接近答案的是这个,这看起来不完整,我还是不明白?!?
罗曼说本来应该在 Froyo 中修复,但我使用的是 Honeycomb,正如您从屏幕截图中看到的那样,它似乎不起作用。任何建议将不胜感激。
When I disable my Spinner it looks almost exactly like it did prior to being disabled, i.e.
Before
After
It is disabled and so functionally everything is fine but I'd like it to look disabled. This question appears to have been asked around the traps (here and here for instance) but the closest anyone's come to an answer is this, which appears incomplete and I don't understand anyway?!?
Romain said it was to be fixed in Froyo onwards but I'm using Honeycomb and as you can see from the screenshots, it doesn't appear to work. Any advice would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我遇到了类似的问题,除了 getChildView 为我返回 null 之外,因此例外的解决方案不起作用。
我相信这是因为我在 XML 中设置了适配器,而这忽略了“可点击”和“已启用”属性。
这是我的 XML:
我的解决方案是删除“已启用”和“可点击”属性
并将以下代码放入我的“onCreate”中
希望它能帮助别人!
I had a similar problem, except getChildView returned null for me, so the excepted solution did not work.
I believe this was caused because I set the adapter in XML, and this ignored the "clickable" and "enabled" attributes.
This was my XML:
The solution for me was to remove the "enabled" and "clickable" attributes
and put the following code in my "onCreate"
Hope it helps someone!
您也可以不进行类型转换,如下所示:
You can do without typecasting also as follows:
我发现这是 @JSPDeveloper01 之前回答过的同一问题的最佳解决方案:
https://stackoverflow.com/a/20401876/8041634
由于 Android 不会将微调器灰显。设置为禁用后,他建议创建一个自定义方法,在微调器上使用
.setAlpha
命令,从而使其中的文本变灰。杰出的。I found this to be the best solution to the same question that was previously answered by @JSPDeveloper01:
https://stackoverflow.com/a/20401876/8041634
Since Android doesn't gray out the spinner when it has been set to disabled, he suggests creating a custom method that uses the
.setAlpha
command on the spinner, which grays out the text within it. Brilliant.为了将来参考,如果您使用 Kotlin,您可以使用扩展函数,并为禁用元素提供自定义行为:
这将允许为微调器子视图设置自定义属性,因为微调器被禁用,而无需子类化。请小心,因为扩展函数是静态解析的!
For future reference, if you're using Kotlin, you can make use of extension functions, and provide a custom behaviour for disabled elements:
This will allow to set custom properties to spinner children views, as the spinner is being disabled, without need for subclassing. Be cautious, as extension functions are resolved statically!
我写了一个小小的 Kotlin 扩展。
I wrote a little Kotlin extension.
不知道你是否还需要这个,但有办法。我自己也一直在为这个问题而苦苦挣扎。我最终做了这样的事情:
这实际上是禁用微调器和显示的所选项目。所选项目很可能是 TextView 并且它应该显示为禁用文本视图。
我正在使用这个并且它有效。但由于某种我不知道的原因,它并不像其他禁用视图那样“灰显”。但它看起来仍然被禁用。尝试一下。
Don't know if you still need this but there is a way. I've been struggling with this issue myself. I ended up doing something like this:
What this actually does is disable the spinner and the selected item that is shown. Most likely the selected item is a TextView and it should show as a disabled TextView.
I am using this and it works. But for some reason unknown to me it is not as "greyed-out" as other disabled views. It still looks disabled though. Try it out.
让旋转器看起来被禁用的一种巧妙方法是降低透明度。
One clever way of making spinners look disabled is to lower the transparency.
如果您要创建具有自定义布局的适配器(即扩展
R.layout.simple_spinner_item
),请将此属性添加到 XML:android:duplicateParentState="true"
If you're creating an adapter with a custom layout (i.e., extending
R.layout.simple_spinner_item
), add this attribute to the XML:android:duplicateParentState="true"
spnr 是我的
Spinner
对象,它通过findViewById(...)
引用 XML 视图文件。spnr is my
Spinner
object which refers to the XML view file, byfindViewById(...)
..getSelectedView()
对我不起作用。所以我欺骗了Spinner
以显示被禁用。您需要为禁用外观定义自己的颜色。
例如:
禁用我的微调器:
启用我的微调器:
您不需要更改样式或修改任何 XML 。只需在代码中执行此操作,即使在事件方法中,也应该没问题。
The
.getSelectedView()
did not work for me. So I tricked theSpinner
to show being disabled.You will need to define your own colors for the disabled look.
For Example:
So to disable my spinner:
To enable my spinner:
You don't need to change styles or modify any XML. Just do this in your code, even within event methods, you should be fine.
我已经尝试了以下操作,并且它按我的预期工作:
I've tried the following, and it's working as expected for me:
这对我有用......
用于禁用微调器
并将其重新启用
this worked for me...
For disabling the spinner
and enabling it back
视图可以由多个可触摸元素组成。您必须将它们全部禁用,如下所示:
如果它是一个简单的,因为它也会返回自身:
View#getTouchables()
Views can be compose by multiple touchable elements. You have to disable them all, like this:
If it is a simple one since it also returns itself:
View#getTouchables()
我的可能是一个特殊情况,要么是因为我设置适配器的顺序,要么是因为我使用了两个自定义微调器类:
Spinner
类。我发现让微调器看起来被禁用的关键是:
setEnabled
函数中的旧对象无效,以及onDraw
函数中设置颜色。在这两个自定义微调器类中,我有一个像这样的特殊
setEnabled
函数,使旧视图无效:我还在每个自定义微调器中重写了
onDraw
函数 类别:Mine may be a special case either due to the order that I'm setting my adapter or due to the fact that I'm using a two custom spinner classes:
LinearLayout
class, andSpinner
class.The keys I found to getting the spinner to look disabled were:
setEnabled
function, andonDraw
function.Inside both of those custom spinner classes, I have a special
setEnabled
function like this, invalidating the old view:I also override the
onDraw
function in my each custom spinner class: