city_data.xml
<?xml version="1.0" encoding="utf-8"?>
<resources> <string-array name="city_labels">
<item>中国-北京</item>
<item>中国-上海</item>
<item>中国-长沙</item>
</string-array> </resources>
============================
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name" >Demo07CityArea
</string>
<string name="action_settings" >Settings
</string>
<string name="city_prompt" >你喜欢的城市:
</string>
<string name="area_prompt" >你喜欢的城区:
</string> </resources>
==============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="horizontal"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"> <!--prompt提示--> <Spinner
android:id="@+id/city"
android:prompt="@string/city_prompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/city_labels"
/> <Spinner
android:id="@+id/area"
android:prompt="@string/area_prompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout> ================================
package com.example.demo07cityarea; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner; public class MainActivity extends Activity {
private Spinner city=null;
private Spinner area=null; private String[][] areaData = new String[][]{
{"朝阳","海淀"},{"黄埔"},{"岳麓","雨花"} }; //下拉列表内容Adapter
private ArrayAdapter<CharSequence> adapterArea=null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); this.city=(Spinner)super.findViewById(R.id.city);
this.area= (Spinner)super.findViewById(R.id.area);
this.city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int idx, long id) {//选项选中时触发
//实例化列表项
MainActivity.this.adapterArea= new ArrayAdapter<CharSequence>(MainActivity.this,android.R.layout.select_dialog_item,MainActivity.this.areaData[idx]);
//设置列表显示风格
MainActivity.this.adapterArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//设置数据
MainActivity.this.area.setAdapter(MainActivity.this.adapterArea);
} @Override
public void onNothingSelected(AdapterView<?> adapterView) { }
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
04-25 21:07