Spinner下拉列表
实现三个下拉按钮,要求选择第一个省份时,第二个城市,第三个景点同步跟踪相对应的省份 在资源文件夹的布局文件中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" tools:context="com.phone.day04_home02.MainActivity" > <Spinner android:id="@+id/s1_sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Spinner android:id="@+id/s2_sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Spinner android:id="@+id/s3_sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> 在MainActivity中的代码,利用适配器完成实现
package com.phone.day04_home02; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; public class MainActivity extends Activity { //创建适配器三个 private ArrayAdapter<String> p_adapter,c_adapter,j_adapter; //创建对应的资源数组,城市和景点采用二维数组 private String[] province={"北京","上海","山东","江苏"}; private String[][] city={{"海淀区","昌平区","朝阳区"},{"黄浦区","浦东新区"},{"济南","青岛","聊城"},{"苏州","南京","常州"}}; private String[][] jingdian={{"故宫","颐和园","圆明园"},{"外滩","豫园","陆家嘴","南京步行街"},{"栈桥","大明湖","趵突泉"},{"苏州园林","南京长江大桥","江南小镇"}}; //声明Spinner的引用 private Spinner s1_sp; private Spinner s2_sp; private Spinner s3_sp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); s1_sp=(Spinner) findViewById(R.id.s1_sp); s2_sp=(Spinner) findViewById(R.id.s2_sp); s3_sp=(Spinner) findViewById(R.id.s3_sp); //构建适配器 p_adapter=new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item,province); c_adapter=new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item); c_adapter.addAll(city[0]); j_adapter=new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item); j_adapter.addAll(jingdian[0]); //将适配器和Spinner链接 s1_sp.setAdapter(p_adapter); s2_sp.setAdapter(c_adapter); s3_sp.setAdapter(j_adapter); //设置省份Spinner的监听器 s1_sp.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub //清空适配器 c_adapter.clear(); j_adapter.clear(); //添加数组资源,跟组对应的位置 c_adapter.addAll(city[position]); j_adapter.addAll(jingdian[position]); //Spinner默认选择重置为第一个 s2_sp.setSelection(0); s3_sp.setSelection(0); } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }); } @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; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
相关推荐
绿豆饼 2020-04-10
狼窝 2020-03-20
Mexican 2011-01-20
满城风絮 2011-09-06
yxx0 2015-04-15
ruanjianxiong 2016-09-20
fanleiym 2017-02-09
virusplayer 2015-04-15
Ifree团队 2013-07-08
emptoney 2013-03-19
toperfect 2013-03-19
yxx0 2013-01-22
tufeizhang 2012-11-30
maplele 2012-07-27
najiutan 2012-02-26
Mexican 2012-02-15