Android:获取string-array中使用的字符串资源的名称

时间:2016-10-13 13:13:51

标签: android arrays xml

我有一个小问题。

  

我的string.xml

<resources>
    <!-- Months -->
    <string name="january">January</string>
    <string name="february">February</string>
    <string name="march">March</string>
    <string name="april">April</string>
    <string name="may">May</string>
    <string name="june">June</string>
    <string name="july">July</string>
    <string name="august">August</string>
    <string name="september">September</string>
    <string name="october">October</string>
    <string name="november">November</string>
    <string name="december">December</string>
</resources>
  

我的month.xml

<resources>
    <string-array name="months">
        <item>@string/january</item>
        <item>@string/february</item>
        <item>@string/march</item>
        <item>@string/april</item>
        <item>@string/may</item>
        <item>@string/june</item>
        <item>@string/july</item>
        <item>@string/august</item>
        <item>@string/september</item>
        <item>@string/october</item>
        <item>@string/november</item>
        <item>@string/december</item>
    </string-array>
</resources>

我为我的微调器设置了month.xml。

<Spinner
    android:id="@+id/sn_months"
    android:layout_width="150dp"
    android:layout_height="50dp"
    android:textAlignment="center"
    android:entries="@array/months"/>

现在,我想获取项目的字符串名称,如1月,2月,...不是1月,2月,...在微调器的onItemSelected中使用项目的位置。我怎么能这样做?

3 个答案:

答案 0 :(得分:0)

{{1}}

答案 1 :(得分:0)

您应该使用spinner.getSelectedItemPosition(),而不是依赖字符串值进行比较,因为它会返回所选项目的索引值。

答案 2 :(得分:0)

1)根据需要更改输入。

<resources>
<!-- Months -->
<string name="january">january</string>
<string name="february">february</string>
<string name="march">march</string>
<string name="april">april</string>
<string name="may">may</string>
<string name="june">june</string>
<string name="july">july</string>
<string name="august">august</string>
<string name="september">september</string>
<string name="october">october</string>
<string name="november">november</string>
<string name="december">december</string>

2)通过扩展Sp​​inner创建自定义视图    改变attributre加载行为