省市级三级联动
前言:
在我们输入信息是经常会让我们输入户籍所在地,此时省市级三级联动就很有必要了
正文:
首先在xml文件中添加如下代码
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.aaaaa.sp.MainActivity"> <TextView android:id="@+id/textview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请选择您所在的城市" android:textSize="20sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" > <Spinner android:id="@+id/provicespinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1"> </Spinner> <Spinner android:id="@+id/cityspinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/provicespinner" android:layout_weight="1"> </Spinner> <Spinner android:id="@+id/countyspinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/cityspinner" android:layout_weight="1"> </Spinner> </LinearLayout> <TextView android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_centerVertical="true" android:gravity="center_horizontal" android:text="显示城市" android:textSize="20sp" /> </RelativeLayout>
Java代码如:
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private Spinner provicespinner; private Spinner cityspinner; private Spinner countyspinner; private ArrayAdapter<String> priviceAdapter; private ArrayAdapter cityAdapter; private ArrayAdapter contyAdapter; private int privicePosition; private int cityPosition; private int countyPosition; private String[] provice = {"山西", "江苏", "安徽"}; private String[][] city = {{"太原", "大同", "阳泉"}, {"徐州", "南京", "苏州"}, {"宿州", "合肥", "淮北"}}; private String[][][] county = {{{"小店区", "迎泽区", "尖草坪区"}, {"阳高县", "天镇县", "浑源县"}, {"城区", "郊区", "矿区"}}, {{"沛县", "丰县", "临空新城"}, {"玄武", "鼓楼", "下关"}, {"虎丘区", "吴中区", "吴江区"}}, {{"砀山", "萧县", "涌泉区"}, {"肥东县", "肥西县", "长丰县"}, {"杜集区", "相山区", "烈山区"}}}; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textview); provicespinner = (Spinner) findViewById(R.id.provicespinner); cityspinner = (Spinner) findViewById(R.id.cityspinner); countyspinner = (Spinner) findViewById(R.id.countyspinner); //显示省 priviceAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, provice); provicespinner.setAdapter(priviceAdapter); //设置省级的监听器--目的是在你选择省后直接显示对应的市,最主要的是应用position provicespinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { cityAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, city[position]); cityspinner.setAdapter(cityAdapter); privicePosition = position; } @Override public void onNothingSelected(AdapterView<?> parent) { } }); //给市spinner设置监听器 cityspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { contyAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, county[privicePosition][position]); countyspinner.setAdapter(contyAdapter); //为能把此时的position传出去 cityPosition = position; } @Override public void onNothingSelected(AdapterView<?> parent) { } }); //为县设置适配器目的是为了显示你算选的省市县 countyspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { //在下面所选内容显示 textView.setText("选择的城市为" + " " + provice[privicePosition] + " " + city[privicePosition][cityPosition] + " " + county[privicePosition][cityPosition][position]); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } }
这样实现其实就是练练手,在实际的操作中可以说用到的东西复杂的多,上面注释解释的很详细就不一步步的解释,重要的就是运用spinner的监听器里面的参数position,从而提取出上面定义的几个数组中数据