Android setting label to spinner

喜你入骨 提交于 2021-02-20 16:03:29

问题


Hi how to set a label in spinner: ie the lable should initially visible when user clicke the spinner button options visible , when user select the option the label should replace with the new item,is it possible with spinner?


回答1:


Spinners do not have "labels". Beyond that, though, what I think you are describing is exactly what a Spinner does:

  • When closed, shows the last selection made by the user, or the initial selection if it has never been opened
  • When opened, shows a selection list of available choices for the user



回答2:


You can put your first item as a label:

<string-array name="countriesList">
    <item>(Select country)</item>
    <item>Country 1</item>
    <item>Country 2...</item>
</string-array>



回答3:


I solved this issue in this way:

mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            if(phonePrefix != null && phonePrefix.getAdapter() != null)
            {
                TextView txt = (TextView) phonePrefix.getSelectedView();
                String str = txt.getText().toString();
                String [] arr = str.split(" ");
                str  = arr[1] + " " + arr[2];
                txt.setText(str);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });



回答4:


I think it's possible. I would be doing it in following way:

  1. Inflate Button with desired text/label
  2. Intercept button pressing
  3. Create on-fly Spinner
  4. Replace Button with Spinner
  5. Be happy


来源:https://stackoverflow.com/questions/4334684/android-setting-label-to-spinner

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