Android 如何获得 Spinner 值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1947933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:54:05  来源:igfitidea点击:

How to get Spinner value?

androidspinner

提问by Sam Dutton

In Android, I am trying to get the selected Spinner value with a listener.

在 Android 中,我试图通过侦听器获取选定的 Spinner 值。

What is the best way to get the spinner's value?

获得微调器值的最佳方法是什么?

回答by dodo

Spinner mySpinner = (Spinner) findViewById(R.id.your_spinner);
String text = mySpinner.getSelectedItem().toString();

回答by Erich Douglass

The Spinner should fire an "OnItemSelected" event when something is selected:

The Spinner should fire an "OnItemSelected" event when something is selected:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        Object item = parent.getItemAtPosition(pos);
    }
    public void onNothingSelected(AdapterView<?> parent) {
    }
});

回答by lenooh

Say this is your xml with spinner entries (ie. titles) and values:

假设这是带有微调条目(即标题)和值的 xml:

<resources>
    <string-array name="size_entries">
        <item>Small</item>
        <item>Medium</item>
        <item>Large</item>
    </string-array>

    <string-array name="size_values">
        <item>12</item>
        <item>16</item>
        <item>20</item>
    </string-array>
</resources>

and this is your spinner:

这是你的微调器:

<Spinner
    android:id="@+id/size_spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:entries="@array/size_entries" />

Then in your code to get the entries:

然后在您的代码中获取条目:

Spinner spinner = (Spinner) findViewById(R.id.size_spinner);
String size = spinner.getSelectedItem().toString(); // Small, Medium, Large

and to get the values:

并获取值:

int spinner_pos = spinner.getSelectedItemPosition();
String[] size_values = getResources().getStringArray(R.array.size_values);
int size = Integer.valueOf(size_values[spinner_pos]); // 12, 16, 20

回答by CommonsWare

Yes, you can register a listener via setOnItemSelectedListener(), as is demonstrated here.

是的,你可以注册通过一个倾听者setOnItemSelectedListener(),足以证明这里

回答by gilix

View view =(View) getActivity().findViewById(controlId);
Spinner spinner = (Spinner)view.findViewById(R.id.spinner1);
String valToSet = spinner.getSelectedItem().toString();

回答by Matt Logan

If you already know the item is a String, I prefer:

如果您已经知道该项目是 a String,我更喜欢:

String itemText = (String) mySpinner.getSelectedItem();

Calling toString()on an Objectthat you know is a Stringseems like a more roundabout path than just casting the Objectto String.

调用toString()一个Object你知道是 a 的方法String似乎比仅仅投射Objectto更迂回String

回答by wafaa hilmy

add setOnItemSelectedListener to spinner reference and get the data like that`

将 setOnItemSelectedListener 添加到微调器引用并获取这样的数据`

 mSizeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
            selectedSize=adapterView.getItemAtPosition(position).toString();