如何禁用微调器中的项目 [英] How to disable an item in a Spinner

查看:88
本文介绍了如何禁用微调器中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将Spinner列表中的特定条目显示为禁用状态?

Is it possible to display particular entries in a Spinner list as disabled?

即,我想始终显示一个包含四个条目(例如,北,南,东和西)的微调框,但是我希望能够禁用其中的任何一个,以便其显示为灰色且不可选择.

I.e., I want to always display a spinner with four entries (North, South, East, and West, say), but I want to be able to disable any one of these so that is appears greyed out and not selectable.

这是可能的,还是我每次都要重新创建列表,而忽略无效的条目?

Is this possible, or will I have to recreate the list each time, leaving out the invalid entries?

推荐答案

   // Create spinner month, and disable month < today
    List<CharSequence> listMonth = new ArrayList<CharSequence>();
    for (int m = 0; m < 12; m++) {
        if (m < 9) {
            listMonth.add("0" + (m + 1));
        } else {
            listMonth.add("" + (m + 1));
        }
    }
        // Create spinner item
    adapterMonth = new ArrayAdapter<CharSequence>(this,
            R.layout.layout_spinner_item, listMonth) {
         // Disable click item < month current
        @Override
        public boolean isEnabled(int position) {
            // TODO Auto-generated method stub
            if (year <= max_year && position < max_month - 1) {
                return false;
            }
            return true;
        }
        // Change color item
        @Override
        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub
            View mView = super.getDropDownView(position, convertView, parent);
            TextView mTextView = (TextView) mView;
            if (year <= max_year && position < max_month - 1) {
                mTextView.setTextColor(Color.GRAY);
            } else {
                mTextView.setTextColor(Color.BLACK);
            }
            return mView;
        }
    };

    adapterMonth
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spn2.setAdapter(adapterMonth);

这篇关于如何禁用微调器中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆