欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现联动下拉框二级地市联动下拉框功能

程序员文章站 2023-10-29 12:52:28
日常使用软件中,为了方便且规范输入,会使用到下拉框进行输入,如注册时生日选项,购物时的地址输入,都会用到下拉框,今日笔者为了巩固已学的知识,实现了二级联动下拉框用作回顾及分...

日常使用软件中,为了方便且规范输入,会使用到下拉框进行输入,如注册时生日选项,购物时的地址输入,都会用到下拉框,今日笔者为了巩固已学的知识,实现了二级联动下拉框用作回顾及分享给求知的新手。

思路/步骤:

在实现联动下拉框之前,我们先对用到的arrayadapter和数据的封装作必要的了解,android 中提供了很多适配器的实现类,其中arrayadapter就其中之一。它可以通过泛型来指定要适配的数据类型,然后在构造函数中把要适配的数据传入。如:

arrayadapter<charsequence> adapter = arrayadapter.createfromresource(
  getapplicationcontext(), 
  r.array.province,
   android.r.layout.simple_spinner_item);

arrayadapter中比较常用的泛型是string,但此处用了charsequence(字符序列),通过指定泛型来装配适合的数据类型。

这段代码中有三个参数:

第一个参数context是上下文,就是当前的activity。
第二个参数是数据来源,r.array.province是存储在xml文件中的数据。
第三个参数是android sdk中内置的一个textview。(此xml文件中只有一个textview)。

通过创建一个arrayadapter处理存储在xml中的省份地市数据,用spinner控件处理arrayadapter处理好的数据,用textview将数据显示出来形成一个list供用户点击选择。

主要实现代码: mainactivity.java

import android.os.bundle;
import android.app.activity;
import android.view.menu;
import android.view.view;
import android.widget.adapterview;
import android.widget.adapterview.onitemselectedlistener;
import android.widget.arrayadapter;
import android.widget.spinner;
import android.widget.toast;
public class mainactivity extends activity {
 spinner spinner1, spinner2;//声明两个下拉框
 string city, province;
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 spinner1 = (spinner) findviewbyid(r.id.spinner1);//绑定id
 spinner2 = (spinner) findviewbyid(r.id.spinner2);//绑定id
 arrayadapter<charsequence> adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.province,
  android.r.layout.simple_spinner_item);//创建资源
 spinner1.setadapter(adapter);//装配数据
 spinner1.setonitemselectedlistener(new provinceclicklisten());//监听事件
 spinner2.setonitemselectedlistener(new cityclicklisten());//监听事件
 }
class provinceclicklisten implements onitemselectedlistener {
 public void onitemselected(adapterview<?> parent, view v, int i, long l) {
  spinner spinner = (spinner) parent;
  string pro = (string) spinner.getitematposition(i);//获取当前选中项
  arrayadapter<charsequence> adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.def,
   r.layout.item);//初始化
  province = spinner1.getselecteditem().tostring();//当前选中的省份
  /** 根据省份,装配地市数据**/
  if (pro.equals("北京")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.北京, r.layout.item);
  } else if (pro.equals("天津")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.天津, r.layout.item);
  } else if (pro.equals("河北")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.河北, r.layout.item);
  } else if (pro.equals("山西")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.山西, r.layout.item);
  } else if (pro.equals("内蒙古")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.内蒙古, r.layout.item);
  } else if (pro.equals("辽宁")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.辽宁, r.layout.item);
  } else if (pro.equals("吉林")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.吉林, r.layout.item);
  } else if (pro.equals("黑龙江")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.黑龙江, r.layout.item);
  } else if (pro.equals("上海")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.上海, r.layout.item);
  } else if (pro.equals("江苏")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.江苏, r.layout.item);
  } else if (pro.equals("浙江")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.浙江, r.layout.item);
  } else if (pro.equals("安徽")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.安徽, r.layout.item);
  } else if (pro.equals("福建")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.福建, r.layout.item);
  } else if (pro.equals("江西")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.江西, r.layout.item);
  } else if (pro.equals("山东")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.山东, r.layout.item);
  } else if (pro.equals("河南")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.河南, r.layout.item);
  } else if (pro.equals("湖北")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.湖北, r.layout.item);
  } else if (pro.equals("湖南")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.湖南, r.layout.item);
  } else if (pro.equals("广东")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.广东, r.layout.item);
  } else if (pro.equals("广西")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.广西, r.layout.item);
  } else if (pro.equals("海南")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.海南, r.layout.item);
  } else if (pro.equals("重庆")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.重庆, r.layout.item);
  } else if (pro.equals("四川")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.四川, r.layout.item);
  } else if (pro.equals("贵州")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.贵州, r.layout.item);
  } else if (pro.equals("云南")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.云南, r.layout.item);
  } else if (pro.equals("*")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.*, r.layout.item);
  } else if (pro.equals("陕西")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.陕西, r.layout.item);
  } else if (pro.equals("甘肃")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.甘肃, r.layout.item);
  } else if (pro.equals("青海")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.青海, r.layout.item);
  } else if (pro.equals("宁夏")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.宁夏, r.layout.item);
  } else if (pro.equals("*")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.*, r.layout.item);
  } else if (pro.equals("*")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.*, r.layout.item);
  } else if (pro.equals("香港")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.香港, r.layout.item);
  } else if (pro.equals("澳门")) {
  adapter = arrayadapter.createfromresource(getapplicationcontext(), r.array.澳门, r.layout.item);
  }
  spinner2.setadapter(adapter);//装配地市数据
 }
 public void onnothingselected(adapterview<?> parent) {

 }
 }
 class cityclicklisten implements onitemselectedlistener {

 public void onitemselected(adapterview<?> parent, view v, int i, long l) {
  city = spinner2.getselecteditem().tostring();//当前选中的地市
  if (city.equals("-城市-")) {//当前还没选择地市
  } else {
  toast.maketext(getapplicationcontext(), province+"省"+city, toast.length_long).show();//toast显示选中的省份城市
  }
 }
 public void onnothingselected(adapterview<?> parent) {}
 }
 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;
 }
}

此外还有布局文件: 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: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=".mainactivity" >
  <textview
    android:id="@+id/province"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="省份:" />
  <spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" />
  <textview
    android:id="@+id/city"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="城市:" />
  <spinner
    android:id="@+id/spinner2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" />
</linearlayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
  <textview xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinner_item"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:text="名称"
    android:textcolor="#000"
    android:textsize="16dp"/>

实现效果如下:

Android实现联动下拉框二级地市联动下拉框功能
Android实现联动下拉框二级地市联动下拉框功能
Android实现联动下拉框二级地市联动下拉框功能
Android实现联动下拉框二级地市联动下拉框功能
Android实现联动下拉框二级地市联动下拉框功能

总结

以上所述是小编给大家介绍的android实现联动下拉框二级地市联动下拉框功能,希望对大家有所帮助