如何将 Spinner 设为“禁用”?状态看起来已禁用?

发布于 2024-12-08 02:07:13 字数 906 浏览 1 评论 0原文

当我禁用 Spinner 时,它看起来几乎与禁用之前一模一样,即

之前

enter image此处描述

之后

在此处输入图像描述

它已被禁用,因此功能上一切都很好,但是我想要它看起来已禁用。这个问题似乎是围绕陷阱提出的(此处此处< /strong> 例如)但是最接近答案的是这个,这看起来不完整,我还是不明白?!?

罗曼说本来应该在 Froyo 中修复,但我使用的是 Honeycomb,正如您从屏幕截图中看到的那样,它似乎不起作用。任何建议将不胜感激。

When I disable my Spinner it looks almost exactly like it did prior to being disabled, i.e.

Before

enter image description here

After

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

漆黑的白昼 2024-12-15 02:07:14

我遇到了类似的问题,除了 getChildView 为我返回 null 之外,因此例外的解决方案不起作用。
我相信这是因为我在 XML 中设置了适配器,而这忽略了“可点击”和“已启用”属性。

这是我的 XML:

<Spinner
android:id="@+id/my_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_large"
android:alpha="0.86"
android:enabled="false"
android:clickable="false"
android:entries="@array/array_of_entries"
android:spinnerMode="dropdown"/>

我的解决方案是删除“已启用”和“可点击”属性
并将以下代码放入我的“onCreate”中

spinner.setEnabled(false);

希望它能帮助别人!

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:

<Spinner
android:id="@+id/my_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_large"
android:alpha="0.86"
android:enabled="false"
android:clickable="false"
android:entries="@array/array_of_entries"
android:spinnerMode="dropdown"/>

The solution for me was to remove the "enabled" and "clickable" attributes
and put the following code in my "onCreate"

spinner.setEnabled(false);

Hope it helps someone!

一桥轻雨一伞开 2024-12-15 02:07:14

您也可以不进行类型转换,如下所示:

new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
             // Depend on your selection check position and disable it
             if(position == 1) {
                view.setEnabled(false);
                view.setEnabled(false);
             }
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
 }

You can do without typecasting also as follows:

new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
             // Depend on your selection check position and disable it
             if(position == 1) {
                view.setEnabled(false);
                view.setEnabled(false);
             }
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
 }
书间行客 2024-12-15 02:07:14

我发现这是 @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.

冰雪之触 2024-12-15 02:07:14

为了将来参考,如果您使用 Kotlin,您可以使用扩展函数,并为禁用元素提供自定义行为:

fun Spinner.forceEnabled(isEnabled : Boolean){
    setEnabled(isEnabled)
    getChildAt(0)?.let{ childView ->
        childView.alpha = if (this.isEnabled) 1.0f else 0.33f
    }
    invalidate()
}

someSpinner.forceEnabled(true)

这将允许为微调器子视图设置自定义属性,因为微调器被禁用,而无需子类化。请小心,因为扩展函数是静态解析的!

For future reference, if you're using Kotlin, you can make use of extension functions, and provide a custom behaviour for disabled elements:

fun Spinner.forceEnabled(isEnabled : Boolean){
    setEnabled(isEnabled)
    getChildAt(0)?.let{ childView ->
        childView.alpha = if (this.isEnabled) 1.0f else 0.33f
    }
    invalidate()
}

someSpinner.forceEnabled(true)

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!

逆流 2024-12-15 02:07:14

我写了一个小小的 Kotlin 扩展。

// android spinner enable/disable doesn't grey out the item.
// this does.
private fun AppCompatSpinner?.setEnabled(enable:Boolean) {
    if (this != null) {
        isEnabled = enable
        alpha = if (enable) 1.0f else 0.5f
    }
}

I wrote a little Kotlin extension.

// android spinner enable/disable doesn't grey out the item.
// this does.
private fun AppCompatSpinner?.setEnabled(enable:Boolean) {
    if (this != null) {
        isEnabled = enable
        alpha = if (enable) 1.0f else 0.5f
    }
}
人疚 2024-12-15 02:07:13

不知道你是否还需要这个,但有办法。我自己也一直在为这个问题而苦苦挣扎。我最终做了这样的事情:

((Spinner) spinner).getSelectedView().setEnabled(false);
spinner.setEnabled(false);

这实际上是禁用微调器和显示的所选项目。所选项目很可能是 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:

((Spinner) spinner).getSelectedView().setEnabled(false);
spinner.setEnabled(false);

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.

远昼 2024-12-15 02:07:13

让旋转器看起来被禁用的一种巧妙方法是降低透明度。

Spinner spinner = (Spinner) findViewById(R.id.my_spinner);
spinner.setEnabled(false);
spinner.setAlpha(0.5f);

One clever way of making spinners look disabled is to lower the transparency.

Spinner spinner = (Spinner) findViewById(R.id.my_spinner);
spinner.setEnabled(false);
spinner.setAlpha(0.5f);
猫性小仙女 2024-12-15 02:07:13

如果您要创建具有自定义布局的适配器(即扩展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"

弄潮 2024-12-15 02:07:13
((Spinner) spnr).getSelectedView().setEnabled(false);
((Spinner) spnr).setEnabled(false);

spnr 是我的 Spinner 对象,它通过 findViewById(...) 引用 XML 视图文件。

((Spinner) spnr).getSelectedView().setEnabled(false);
((Spinner) spnr).setEnabled(false);

spnr is my Spinner object which refers to the XML view file, by findViewById(...).

九厘米的零° 2024-12-15 02:07:13

.getSelectedView() 对我不起作用。所以我欺骗了 Spinner显示被禁用。

您需要为禁用外观定义自己的颜色。

例如:

R.color.blue_text //means enabled
R.color.gray_text //means disabled

禁用我的微调器:

((TextView)mySpinner.getChildAt(0)).setTextColor(getResources().getColor(R.color.gray_text));
mySpinner.setEnabled(false);
mySpinner.setFocusable(false);

启用我的微调器:

((TextView)mySpinner.getChildAt(0)).setTextColor(getResources().getColor(R.color.blue_text));
mySpinner.setEnabled(true);
mySpinner.setFocusable(true);

您不需要更改样式或修改任何 XML 。只需在代码中执行此操作,即使在事件方法中,也应该没问题。

The .getSelectedView() did not work for me. So I tricked the Spinner to show being disabled.

You will need to define your own colors for the disabled look.

For Example:

R.color.blue_text //means enabled
R.color.gray_text //means disabled

So to disable my spinner:

((TextView)mySpinner.getChildAt(0)).setTextColor(getResources().getColor(R.color.gray_text));
mySpinner.setEnabled(false);
mySpinner.setFocusable(false);

To enable my spinner:

((TextView)mySpinner.getChildAt(0)).setTextColor(getResources().getColor(R.color.blue_text));
mySpinner.setEnabled(true);
mySpinner.setFocusable(true);

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.

嘿哥们儿 2024-12-15 02:07:13

我已经尝试了以下操作,并且它按我的预期工作:

_userMembership.setEnabled(false);
_userMembership.setClickable(false);
_userMembership.setAlpha((float)0.7);
_userMembership.setBackgroundColor(Color.GRAY);

I've tried the following, and it's working as expected for me:

_userMembership.setEnabled(false);
_userMembership.setClickable(false);
_userMembership.setAlpha((float)0.7);
_userMembership.setBackgroundColor(Color.GRAY);
星星的轨迹 2024-12-15 02:07:13

这对我有用......
用于禁用微调器

your_spinner.getSelectedView();
your_spinner.setEnabled(false);

并将其重新启用

your_spinner.setEnabled(true);

this worked for me...
For disabling the spinner

your_spinner.getSelectedView();
your_spinner.setEnabled(false);

and enabling it back

your_spinner.setEnabled(true);
╰◇生如夏花灿烂 2024-12-15 02:07:13

视图可以由多个可触摸元素组成。您必须将它们全部禁用,如下所示:

for(View lol : your_spinner.getTouchables() ) {
    lol.setEnabled(false);
}

如果它是一个简单的,因为它也会返回自身:

查找并返回该视图后代的所有可触摸视图,如果该视图本身是可触摸的,则可能包括该视图。

View#getTouchables()

Views can be compose by multiple touchable elements. You have to disable them all, like this:

for(View lol : your_spinner.getTouchables() ) {
    lol.setEnabled(false);
}

If it is a simple one since it also returns itself:

Find and return all touchable views that are descendants of this view, possibly including this view if it is touchable itself.

View#getTouchables()

戏蝶舞 2024-12-15 02:07:13

我的可能是一个特殊情况,要么是因为我设置适配器的顺序,要么是因为我使用了两个自定义微调器类:

  1. 第一个类扩展了< code>LinearLayout 类,
  2. 第二个扩展了 Spinner 类。

我发现让微调器看起来被禁用的关键是:

  1. 使 setEnabled 函数中的旧对象无效,以及
  2. onDraw 函数中设置颜色。

在这两个自定义微调器类中,我有一个像这样的特殊 setEnabled 函数,使旧视图无效:

public void setEnabled(Boolean enabled) {
    super.setEnabled(enabled);
    invalidate();
}

我还在每个自定义微调器中重写了 onDraw 函数 类别:

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (this.getChildAt(0) != null) {
        this.getChildAt(0).setAlpha(this.isEnabled() ? 1.0f : 0.7f);
    }
}

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:

  1. The first class extends the LinearLayout class, and
  2. The second extends the Spinner class.

The keys I found to getting the spinner to look disabled were:

  1. Invalidating the old object in the setEnabled function, and
  2. Setting the color in the onDraw function.

Inside both of those custom spinner classes, I have a special setEnabled function like this, invalidating the old view:

public void setEnabled(Boolean enabled) {
    super.setEnabled(enabled);
    invalidate();
}

I also override the onDraw function in my each custom spinner class:

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (this.getChildAt(0) != null) {
        this.getChildAt(0).setAlpha(this.isEnabled() ? 1.0f : 0.7f);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文