activity_main.xml
<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="vertical" >
<Spinner
android:id="@+id/springxz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/sheng_array"/>
<Spinner
android:id="@+id/springx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/springInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity{
//效果一:
private TextView springInfo;
private Spinner springxz;
//效果二
private Spinner springx;
private String[][] shiArray=new String[][]{{"太原","大同","石家庄"},{"郑州","开封","洛阳"},{"西安","宝鸡","咸阳"}};
private ArrayAdapter<CharSequence> adapter=null;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
springxz=(Spinner) findViewById(R.id.springxz);
springx=(Spinner) findViewById(R.id.springx);
springInfo=(TextView) findViewById(R.id.springInfo);
springxz.setOnItemSelectedListener(new MySpringListener());
}
/**
* Spinner
* @author Administrator
*
*/
private class MySpringListener implements OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
try {
//效果一:
/*String pitchOn=arg0.getItemAtPosition(arg2).toString();
springInfo.setText("选择的省份为:"+pitchOn);*/
//效果二:
adapter = new ArrayAdapter<CharSequence>(MainActivity.this,
android.R.layout.simple_spinner_item, shiArray[arg2]);
springx.setAdapter(adapter);
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
}