问题
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:
- Inflate Button with desired text/label
- Intercept button pressing
- Create on-fly Spinner
- Replace Button with Spinner
- Be happy
来源:https://stackoverflow.com/questions/4334684/android-setting-label-to-spinner