代码:
package com.example.spinnerdemo; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String[] start = { "北京", "上海", "成都", "其他" }; String[] target = { "北京", "上海", "成都", "其他" }; Spinner spinner1 = (Spinner) findViewById(R.id.spinner1); spinner1.setAdapter(new MyAdapter1(this, android.R.layout.simple_list_item_1, android.R.id.text1, start)); Spinner spinner2 = (Spinner) findViewById(R.id.spinner2); spinner2.setAdapter(new MyAdapter2(this, android.R.layout.simple_list_item_1, android.R.id.text1, target)); } private class MyAdapter1 extends ArrayAdapter { private LayoutInflater infalter; private String[] start; private int resource; private int textViewResourceId; public MyAdapter1(Context context, int resource, int textViewResourceId, String[] objects) { super(context, resource, textViewResourceId, objects); this.start = objects; this.resource = resource; this.textViewResourceId = textViewResourceId; infalter = LayoutInflater.from(context); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) convertView = infalter.inflate(resource, null); TextView text = (TextView) convertView .findViewById(textViewResourceId); text.setText("pos:"+position); return convertView; } @Override public int getCount() { return start.length; } } private class MyAdapter2 extends ArrayAdapter { private LayoutInflater infalter; private int resource; private int textViewResourceId; private String[] target; public MyAdapter2(Context context, int resource, int textViewResourceId, String[] objects) { super(context, resource, textViewResourceId, objects); this.resource = resource; this.textViewResourceId = textViewResourceId; target = objects; infalter = LayoutInflater.from(context); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) convertView = infalter.inflate(resource, null); TextView text = (TextView) convertView .findViewById(textViewResourceId); text.setText("选择了:" + target[position]); text.setBackgroundColor(Color.GREEN); return convertView; } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { if (convertView == null) convertView = infalter.inflate( android.R.layout.simple_list_item_1, null); TextView text = (TextView) convertView .findViewById(android.R.id.text1); text.setText(target[position]); text.setBackgroundColor(Color.RED); return convertView; } @Override public int getCount() { return target.length; } } }
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context="com.example.spinnerdemo.MainActivity" > <Spinner android:id="@+id/spinner1" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Spinner android:id="@+id/spinner2" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>