Java 如何获得 Spinner 的选定值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2652414/
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-13 10:22:37  来源:igfitidea点击:

How do you get the selected value of a Spinner?

javaandroidspinner

提问by Matthew Hall

I am trying to get the selected items string out of a Spinner. So far I have gotten this:

我正在尝试从Spinner. 到目前为止,我已经得到了这个:

bundle.putString(ListDbAdapter.DB_PRI, v.getText().toString());

This does not work and gives a class casting exception (I thought I could cast a Viewto a widget that inherits it. Obviously not!) So how do you get the selected value of a Spinner?

这不起作用并给出了类转换异常(我以为我可以将 aView转换为继承它的小部件。显然不是!)那么如何获得 a 的选定值Spinner

采纳答案by jalopaba

To get the selected value of a spinner you can follow this example.

要获取微调器的选定值,您可以按照此示例进行操作

Create a nested class that implements AdapterView.OnItemSelectedListener. This will provide a callback method that will notify your application when an item has been selected from the Spinner.

创建一个实现 AdapterView.OnItemSelectedListener 的嵌套类。这将提供一个回调方法,当从 Spinner 中选择了一个项目时,该方法将通知您的应用程序。

Within "onItemSelected" method of that class, you can get the selected item:

在该类的“onItemSelected”方法中,您可以获得所选项目:

public class YourItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        String selected = parent.getItemAtPosition(pos).toString();
    }

    public void onNothingSelected(AdapterView parent) {
        // Do nothing.
    }
}

Finally, your ItemSelectedListener needs to be registered in the Spinner:

最后,您的 ItemSelectedListener 需要在 Spinner 中注册:

spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

回答by Rich

You have getSelectedXXX methods from the AdapterView class from which the Spinner derives:

你有来自 AdapterView 类的 getSelectedXXX 方法,Spinner 从中派生:

getSelectedItem()

getSelectedItem()

getSelectedItemPosition()

getSelectedItemPosition()

getSelectedItemId()

getSelectedItemId()

回答by Tivie

Depends ay which point you wish to "catch" the value.

取决于您希望“捕获”值的哪一点。

For instance, if you want to catch the value as soon as the user changes the spinner selected item, use the listener approach (provided by jalopaba)

例如,如果您想在用户更改微调器所选项目时立即捕获该值,请使用侦听器方法(由 jalopaba 提供)

If you rather catch the value when a user performs the final task like clicking a Submit button, or something, then the answer provided by Rich is better.

如果您更愿意在用户执行最终任务(例如单击“提交”按钮或其他操作)时捕获该值,那么 Rich 提供的答案会更好。

回答by Chrispix

mySpinner.getItemAtPosition(mySpinner.getSelectedItemPosition())works based on Rich's description.

mySpinner.getItemAtPosition(mySpinner.getSelectedItemPosition())作品基于 Rich 的描述。

回答by Tharaka Devinda

Simply use this:

只需使用这个:

spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString();

This will give you the Stringof the selected item in the Spinner.

这会给你String所选择的项目中Spinner

回答by Sasa

This is another way:

这是另一种方式:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int pos, long arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

回答by dandev91

To get just the string value within the spinner use the following:

要仅获取微调器中的字符串值,请使用以下命令:

spinner.getSelectedItem().toString();

回答by Adnan Ali

Spinner spinner=(Spinner) findViewById(R.id.spinnername);
String valueinString = spinner.getSelectedItem().toString();

In Case Spinner values are intthe typecast it to int

在 Case Spinner 值是int它的类型转换int

int valueinInt=(int)(spinner.getSelectedItem());