由於以上user45678的回答,我寫了這個
public class SpinnerNoDefault extends Spinner {
public void initialize(List<String> items) {
initialize(items, android.R.layout.simple_spinner_item, android.R.layout.simple_spinner_dropdown_item);
}
public void initialize(List<String> items, @LayoutRes int mItemResId, @LayoutRes int dropdownResId) {
List<String> itemsWithEmpty = new ArrayList<>(items);
itemsWithEmpty.add("");
String[] choicesArray = itemsWithEmpty.toArray(new String[itemsWithEmpty.size()]);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getContext(), mItemResId, choicesArray) {
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
View v = super.getView(position, convertView, parent);
if (position == getCount()) {
((TextView)v.findViewById(android.R.id.text1)).setText(null);
((TextView)v.findViewById(android.R.id.text1)).setHint(parent.getContext().getString(R.string.spinner_no_default_hint));
}
return v;
}
@Override
public int getCount() {
// Don't display last item. It is used as hint.
return super.getCount()-1;
}
};
spinnerArrayAdapter.setDropDownViewResource(dropdownResId);
setAdapter(spinnerArrayAdapter);
setSelection(itemsWithEmpty.size()-1);
}
}
空的意思你要設置 – Pavya
@ user3676184 - 空意味着「空」值或「無」字符串 – user45678
沒有魯斯塔姆,我不想清除我的名單,我只是想微調的默認值是空的,意味着價值將只一旦用戶選擇顯示,直到這時微調將沒有任何價值 – user45678