Android : How to make a spinner disabled look disa

2019-08-04 16:49发布

Possible duplicate of How do I make a Spinner's "disabled" state look disabled?.

I tried the solution mentioned in the post, ie

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

However getSelectedView() returns null. I also tried manually selecting an item and than calling getSelectedView but it still returns null.

4条回答
Deceive 欺骗
2楼-- · 2019-08-04 17:32

Create a drawable selector in the res/drawable folder like this

custonspinner.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <!-- disabled state -->
  <item android:state_enabled="false" android:color="#9D9FA2" /> 
  <item android:color="#000"/>
</selector>

// You can give the drawable also instead of colors in the selector

In your spinner set the background resource like this

android:backgroundResource="@drawable/customspinner"
查看更多
该账号已被封号
3楼-- · 2019-08-04 17:36

@JSPDeveloper01's answer didn't quite work for me (possibly 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: the first class extends the LinearLayout class; the second extends the Spinner class). The keys are invalidating the old object in the setEnabled function and 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 (using part of JSPDeveloper01's answer):

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

    if (this.getChildAt(0) != null) {
        this.getChildAt(0).setAlpha(this.isEnabled() ? 1.0f : 0.7f);
    }
}
查看更多
Juvenile、少年°
4楼-- · 2019-08-04 17:37

One way to do this is to write your own SpinnerAdapter (e.g. an ArrayAdapter) and then override the getView() method. Usually that method inflates the android.R.layout.simple_spinner_item layout and binds the TextView to your data (array in the case of ArrayAdapter). You could take that layout (copy it from your Android SDK folder) and create a disabled version and then do something like that:

private static final int SPINNER_ENABLED = android.R.layout.simple_spinner_item;
private static final int SPINNER_DISABLED = R.layout.simple_spinner_item_disabled;
    @Override
    public final View getView(int position, View convertView, ViewGroup parent) {
        View spinnerView = View.inflate(getContext(), isEnabled(position) ? SPINNER_ENABLED : SPINNER_DISABLED, null);
        TextView textView = (TextView) spinnerView.findViewById(R.id.text1);
        textView.setText(getItem(position).toString());
        return spinnerView;
    }

The android.R.layout.simple_spinner_item looks like this:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/spinnerItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee" />
查看更多
不美不萌又怎样
5楼-- · 2019-08-04 17:40

I figured out the simplest way:

private void setSpinnerEnabled(Spinner spinner, boolean enabled) {
    spinner.setEnabled(enabled);
    spinner.setAlpha(enabled ? 1.0f : 0.4f);
}

It won't change the color or font, just make it a little more transparent...

查看更多
登录 后发表回答