3 つの項目を持つスピナーがあり、XML 文字列配列リソースを使用してフィードします。アクティビティを開くと、スピナーは通常、配列リストにある最初のアイテムを表示します。それを変更して、アイテムが選択される前に、スピナーに「1 つ選択」というテキストを表示したいと思います。
どうやってやるの?
あなたは2つの方法のうちの1つをすることができます。
1)xmlの最初の項目として「SelectOne」を追加し、それを選択として無視するようにリスナーをコーディングします。
2)カスタムアダプタを作成して、最初の行として挿入します。
編集
あなたのリソースで
<string-array name="listarray">
<item>Select One</item>
<item>Item One</item>
<item>Item Two</item>
<item>Item Three</item>
</string-array>
onItemSelectedリスナーの場合:
spinnername.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos == 0) {
}else {
// Your code to process the selection
}
}
});
スピナーのデフォルトテキストを設定するには、スピナーに使用する必要がありandroid:prompt=@string/SelectOne
ます。SelectOneはstring.xmlで定義されています。
例 :
<Spinner android:id="@+id/spinnerTest"
android:layout_marginLeft="50px"
android:layout_width="fill_parent"
android:drawSelectorOnTop="true"
android:layout_marginTop="5dip"
android:prompt="@string/SelectOne"
android:layout_marginRight="30px"
android:layout_height="35px"
/>