how to create an unselectable hint text for Spinner in android ? (without reflection)

别说谁变了你拦得住时间么 提交于 2019-12-12 03:49:25

问题


I have a spinner with drop down mode . I display my list of items with my custom adapter in that. But now i want to use a hint text for my spinner that is unselectable. How can i do this ? thanks in advance


回答1:


You have to add first item like i did in my code then on click of spinner it will hide so user will not able to select that first item

ArrayList cityArraList = new ArrayList();
cityArrayList.add("Select City"); 
Spinner citySpinner = (Spinner) findViewById(R.id.citySpinner);
        final ArrayAdapter<City> cityAdapter = new ArrayAdapter<City>(getActivity(),R.layout.my_simple_list_item_1,cityArrayList){
            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                View v = null;

                // If this is the initial dummy entry, make it hidden
                if (position == 0) {
                    TextView tv = new TextView(getContext());
                    tv.setHeight(0);
                    tv.setVisibility(View.GONE);
                    v = tv;
                } else {
                    // Pass convertView as null to prevent reuse of special case views
                    v = super.getDropDownView(position, null, parent);
                }

                // Hide scroll bar because it appears sometimes unnecessarily, this does not prevent scrolling
                //parent.setVerticalScrollBarEnabled(false);
                return v;
            }
        };


来源:https://stackoverflow.com/questions/40339499/how-to-create-an-unselectable-hint-text-for-spinner-in-android-without-reflec

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!