可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
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.
回答2:
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"
回答3:
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);
回答4:
((Spinner) spnr).getSelectedView().setEnabled(false);
((Spinner) spnr).setEnabled(false);
spnr is my Spinner
object which refers to the XML view file, by findViewById(...)
.
回答5:
this worked for me...
For disabling the spinner
your_spinner.getSelectedView();
your_spinner.setEnabled(false);
and enabling it back
your_spinner.setEnabled(true);
回答6:
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.
回答7:
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);
回答8:
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()
回答9:
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:
- The first class extends the
LinearLayout
class, and
- The second extends the
Spinner
class.
The keys I found to getting the spinner to look disabled were:
- 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:
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (this.getChildAt(0) != null) {
this.getChildAt(0).setAlpha(this.isEnabled() ? 1.0f : 0.7f);
}
}
回答10:
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!
回答11:
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) {
}
}
回答12:
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.