0

正如 jalopaba 在这里演示的那样,我已经创建了一个新类:如何获得 Spinner 的选定值?

public class MyItemSelectedListener 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.
    }
}

并将其注册到原始类中的微调器:

spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

但是,我仍然不能使用该选定的字符串来在同一个类中填写我的代码:

textView.setText(selected);

反正我是这个Android的新手,所以这个问题对你们中的一些人来说可能太愚蠢了

4

4 回答 4

2

在 onItemSelected 中添加 setText 代码:

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    yourTextView.setText(parent.getSelectedItem().toString);
}
于 2012-08-01T05:36:25.913 回答
0

试试这段代码,希望对你有帮助。

final CharSequence[] array_min = {"No 

  Min","100","200","300","400", "500","600","700","800","900","1000",
                  "1100","1200","1300","1400","1500","1600","1700","1800","1900","2000","2500","3000","3500" };

        Spinner s = (Spinner) layout.findViewById(R.id.viewSpin);

        adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, array_min);

        s.setAdapter(adapter);
        s.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View arg1,
                    int arg2, long arg3)
            {
                selItem = parent.getSelectedItem().toString();
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });
于 2012-08-01T05:28:01.280 回答
0

注册微调器后,您可以从 getSelectedItem() 方法对任何操作(如 ocClick()

 regType = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.regType, android.R.layout.simple_spinner_dropdown_item);
    regType.setAdapter(adapter);

public void onClick(View v) { switch (v.getId()) { case R.id.btnSave:

        intent.putExtra("regtype",regType.getSelectedItem().toString());
        startActivity(intent);  

            break;}}
于 2012-08-01T05:41:56.800 回答
0

Use global variable..make following changes in your code

String selected="";

public class MyItemSelectedListener implements OnItemSelectedListener {

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

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

}

textView.setText(selected);

于 2012-08-01T05:39:35.507 回答