如何从android中的spinner获取所选项目

时间:2016-05-16 02:16:59

标签: android spinner

在android中我使用微调器从项列表中选择一个项目。现在我必须在java中使用该选定的项目。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:2)

试试这个!!!

spin.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            String yourName=spiner.getSelectedItem().toString();

        }

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

        }
    });

如果上面的代码对您没有帮助。试试这个!!

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

答案 1 :(得分:0)

在Java中,您可以使用JComboBox显示列表项并进行选择。 例如:

String[] numbers= { "One", "Two", "Three", "Four", "Five" };
//Create the combo box, select item at index 4.
//Indices start at 0, so 4 specifies the 'Five'.
JComboBox clst= new JComboBox(numbers);
petList.setSelectedIndex(4);
petList.addActionListener(this);