Android setting label to spinner
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:
- Inflate Button with desired text/label
- Intercept button pressing
- Create on-fly Spinner
- Replace Button with Spinner
- Be happy
精彩评论