• 三级联动


    1.demo.xml布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="小区"
    android:textColor="#000000" />

    <Spinner
    android:id="@+id/sheng"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="楼号+单元号"
    android:textColor="#000000" />

    <Spinner
    android:id="@+id/shi"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="房号"
    android:textColor="#000000" />

    <Spinner
    android:id="@+id/xian"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

    <TextView
    android:id="@+id/t_v"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

    </LinearLayout>

    2.MainActivity.java文件
    public class MainActivity extends AppCompatActivity {

    Spinner s_sheng;
    ArrayAdapter<String> adapter_sheng;
    ArrayAdapter<String> adapter_shi;
    ArrayAdapter<String> adapter_xian;
    Spinner s_shi;
    Spinner s_xian;
    int sheng_postion;
    int shi_postion;
    String seletced_sheng;
    String seletced_shi;
    String seletced_xian;
    TextView t_v;
    String[] arr_sheng = {"A小区", "B小区", "C小区", "D小区", "E小区", "F小区", "G小区"};
    String[][] arr_shi = new String[][]{
    {"1号楼1单元", "1号楼2单元", "1号楼3单元"},
    {"2号楼1单元", "2号楼2单元", "2号楼3单元"},
    {"3号楼1单元", "3号楼2单元", "3号楼3单元"},
    {"4号楼1单元", "4号楼2单元", "4号楼3单元"},
    {"5号楼1单元", "5号楼2单元", "5号楼3单元"},
    {"6号楼1单元", "6号楼2单元", "6号楼3单元"},
    {"7号楼1单元", "7号楼2单元", "7号楼3单元"},       {"8号楼1单元", "8号楼2单元", "8号楼3单元"} }; String[][][] arr_xian = new String[][][]{
    {//
    {"0101", "0102", "0103", "0104"} },
    {// {"0201", "0202", "0203", "0204"} },
    {// {"0301", "0302", "0303", "0304"} },
    {// {"0401", "0402", "0403", "0404"} },
    {// {"0501", "0502", "0503", "0504"} },
    {// {"0601", "0602", "0603", "0604"} },
    {// {"0701", "0702", "0703", "0704"} },
    {// {"0801", "0802", "0803", "0804"} }
    };
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View rootView = View.inflate(this, R.layout.demo, null);
    setContentView(rootView);
    t_v = (TextView) findViewById(R.id.t_v);
    s_sheng = (Spinner) findViewById(R.id.sheng);
    s_shi = (Spinner) findViewById(R.id.shi);
    s_xian = (Spinner) findViewById(R.id.xian);
    adapter_sheng = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, arr_sheng);
    adapter_shi = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, arr_shi[0]);
    adapter_xian = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, arr_xian[0][0]);
    s_sheng.setAdapter(adapter_sheng);
    s_sheng.setSelection(0);
    s_shi.setAdapter(adapter_shi);
    s_shi.setSelection(0);
    s_xian.setAdapter(adapter_xian);
    s_xian.setSelection(0);
    s_sheng.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    Log.e("AAAAAAAAAAAA ", sheng_postion + " " + i);
    sheng_postion = i;
    adapter_shi = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, arr_shi[i]);
    s_shi.setAdapter(adapter_shi);
    seletced_sheng = arr_sheng[i];
    }
    @Override
    public void onNothingSelected(AdapterView<?> parent) { } });
    s_shi.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {//
    shi_postion = i;
    adapter_xian = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, arr_xian[sheng_postion][shi_postion]);
    s_xian.setAdapter(adapter_xian);
    seletced_shi = arr_shi[sheng_postion][i];
    }
    @Override
    public void onNothingSelected(AdapterView<?> parent) { } });
    s_xian.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    seletced_xian = arr_xian[sheng_postion][shi_postion][i];
    t_v.setText(seletced_sheng + "-" + seletced_shi + "-" + seletced_xian);
    }
    @Override
    public void onNothingSelected(AdapterView<?> parent) { } }); }}


  • 相关阅读:
    HDU 2023题解分析
    Java中常见的几种类型转换
    Software Version --hdu1976
    单词数
    Usaco 2.3 Zero Sums(回溯DFS)--暴搜
    9的余数
    mongodb学习(一)
    svg学习(九)path
    svg学习(八)polyline
    qunit学习(一)
  • 原文地址:https://www.cnblogs.com/xiaoshumiao/p/8986946.html
Copyright © 2020-2023  润新知