Android 如何禁用微调器中的项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3574827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:49:28  来源:igfitidea点击:

How to disable an item in a Spinner

android

提问by FrinkTheBrave

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

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

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?

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

回答by Luan Dang

   // 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);

回答by T. Aslam

    public class MySpinnerAdapter extends BaseAdapter {

    @Override
    public void isEnabled(int position) {
       return getItem(position).isEnabled(); 
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

      if(convertView == null) {
          convertView = View.inflate(parent.getContext(), R.layout.item, null);
      }
      if(!isEnabled(position)) {
          convertView.setEnabled(false);
          convertView.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                //NO-OP: Just intercept click on disabled item
             }
          });
      }

      return convertView;
    }
}

回答by Haphazard

Someone just posted a solution to your problem, refer to this old post.

有人刚刚发布了您问题的解决方案,请参阅此旧帖子

回答by CommonsWare

Is this possible

这可能吗

It doesn't appear so. The way you would do that with ListViewinvolves areAllItemsEnabled()and isEnabled(). However, those are methods on the ListAdapterinterface, not the SpinnerAdapterinterface. So, I suspect they will be ignored by a Spinner.

看起来不是这样。你会做与方式ListView包括areAllItemsEnabled()isEnabled()。但是,这些是ListAdapter接口上的方法,而不是SpinnerAdapter接口。所以,我怀疑它们会被Spinner.

回答by Levon Petrosyan

class SpinnerAdapter(context: Context, resource: Int, list: ArrayList<String>)
    : ArrayAdapter<String>(context, resource, list) {

        override fun isEnabled(position: Int): Boolean {
            // select position to disable 
            return position != 0
        }

}