Spinner Edittext

时间:2018-04-01 21:13:01

标签: java android android-edittext android-spinner

我对Android工作室相当新,我正在努力制作一个得分应用程序,到目前为止我已经设法创建了一个微调器,允许我为我的拳击应用程序选择一定数量的回合。我的问题是如何将选定数量的轮数转换为选定数量的edittext框

<resources>



    <string-array name="rounds">
        <item>3 Rounds</item>
        <item>4 Rounds</item>
        <item>5 Rounds</item>
        <item>6 Rounds</item>
        <item>8 Rounds</item>
        <item>10 Rounds</item>
        <item>12 Rounds</item>
        <item>15 Rounds</item>


    </string-array>
</resources>



public class insertname extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insertname);


        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.rounds, android.R.layout.simple_spinner_dropdown_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(adapter);
    }
}


  <Spinner
            android:id="@+id/spinner"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />

1 个答案:

答案 0 :(得分:1)

尝试添加此内容,

<integer-array name="values">
    <item>3</item>
    <item>4</item>
    <item>5</item>
    <item>6</item>
    <item>8</item>
    <item>10</item>
    <item>12</item>
    <item>15</item>
</string-array>

选择项目时获取项目的位置并获取整数数组的值。

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            int value =  getResources().getIntArray(R.array.values)[position];
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });