1#
安卓Developer QQ群273843464欢迎您的加入~~

可构建适配器adaper将数组设到adapter显示(例子可去群共享下载)

原文参考:http://blog.cfxixi.com/post/2012/10/17/%E5%AE%89%E5%8D%93%E4%B8%8B%E6%8B%89%E5%88%97%E8%A1%A8spinner%E7%AE%80%E5%8D%95%E7%94%A8%E6%B3%95.aspx

private static final String[]    m_Countries    = { "O", "A", "B", "AB", "其他" };

    private TextView                m_TextView;
    private Spinner                    m_Spinner;
    private ArrayAdapter    adapter;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        m_TextView = (TextView) findViewById(R.id.TextView1);
        m_Spinner = (Spinner) findViewById(R.id.Spinner1);

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

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        
        m_Spinner.setAdapter(adapter);

        m_Spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3)
            {
                m_TextView.setText("aaa" + m_Countries[arg2]);

                arg0.setVisibility(View.VISIBLE);
            }

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

        });
    }


布局示例:


    androidrientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    android:id="@+id/TextView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
  android:id="@+id/Spinner1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
/>