菜单联动功能:
value文件夹下-->array.xml-->数据:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--数据-->
<string-array name="province">
<item>江苏</item>
<item>安徽</item>
</string-array>
<string-array name="js_city">
<item>南京</item>
<item>苏州</item>
<item>盐城</item>
</string-array>
<string-array name="ah_city">
<item>合肥</item>
<item>芜湖</item>
<item>马鞍山</item>
</string-array>
</resources>
public class spinner_ZY extends AppCompatActivity {
private String province;
private String city;
private Spinner spinner1, spinner2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_zy);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner1.setOnItemSelectedListener(new provinceClickListen());
spinner2.setOnItemSelectedListener(new cityClickListen());
}
class provinceClickListen implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parant, View view, int i, long id) {
Spinner spinner = (Spinner) parant;
String pro = (String) spinner.getItemAtPosition(i);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(getApplicationContext(), R.array.province, android.R.layout.simple_spinner_item);
spinner1.setAdapter(adapter);
province = spinner1.getSelectedItem().toString();
if (pro.equals("江苏")) {
adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.js_city, R.layout.support_simple_spinner_dropdown_item);
} else if (pro.equals("安徽")) {
adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.ah_city, R.layout.support_simple_spinner_dropdown_item);
}
spinner2.setAdapter(adapter);
}
public void onNothingSelected(AdapterView<?> parent) { }
}
class cityClickListen implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parant, View view, int i, long id) {
city = spinner2.getSelectedItem().toString();
if (city.equals("-城市-")) {
} else {
Toast.makeText(getApplicationContext(), province + "省" + city, Toast.LENGTH_LONG).show();
}
}
public void onNothingSelected(AdapterView<?> adapterView) { }
}
}
//xml页面:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!--entries属性:spinner_data1,获取value中xml的文件中菜单,
给了下拉菜单值:
android:entries="@array/spinner_data1"-->
<TextView
android:id="@+id/province"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_centerVertical="true"
android:text="省份" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:entries="@array/province"
android:layout_weight="1" />
<TextView
android:id="@+id/city"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_centerVertical="true"
android:text="城市" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:entries="@array/js_city"
/>
</LinearLayout>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/96a7c5099785c64a1b6d6d5bbf46451d.png#pic_center)