开发者

Android setting label to spinner

开发者 https://www.devze.com 2023-01-27 19:32 出处:网络
Hi how to set a label in spinner: ie the lable should initially visible when user click开发者_Go百科e the spinner button options visible , when user select the option the label should replace with th

Hi how to set a label in spinner: ie the lable should initially visible when user click开发者_Go百科e the spinner button options visible , when user select the option the label should replace with the new item,is it possible with spinner?


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


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>


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) {

        }
    });


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号