Android使用Spinner实现城市级联下拉框

Android使用Spinner实现城市级联下拉框,第1张

概述最近写一个使用Spinner实现城市级联下拉框的Dome,现在总结一下,第一次写博客,互相学习。

最近写一个使用Spinner实现城市级联下拉框的Dome,现在总结一下,第一次写博客,互相学习。

activity_main.xml里面有三个Spinner

<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:background="@color/white"   androID:orIEntation="horizontal"   tools:context=".MainActivity">    <Spinner     androID:ID="@+ID/spinner1"     androID:layout_wIDth="match_parent"     androID:layout_height="wrap_content"     androID:layout_weight="1" />    <Spinner     androID:ID="@+ID/spinner2"     androID:layout_wIDth="match_parent"     androID:layout_height="wrap_content"     androID:layout_weight="1"     androID:visibility="invisible" />    <Spinner     androID:ID="@+ID/spinner3"     androID:layout_wIDth="match_parent"     androID:layout_height="wrap_content"     androID:layout_weight="1"     androID:visibility="invisible" />  </linearLayout> 

Spinner的每一个item布局,里面只有一个TextVIEw

<?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:background="@color/white"   androID:gravity="center"   androID:orIEntation="horizontal"   androID:padding="5dp">    <TextVIEw     androID:ID="@+ID/txt_name"     androID:layout_wIDth="match_parent"     androID:layout_height="wrap_content"     androID:text="名称"     androID:textSize="16dp" /> </linearLayout> 

下面是SpinnerAdapter,一般会用到Adapter的有如下几个控件

(1)列表视图控件-ListVIEw
(2)缩略图浏览器控件-gallery
(3)网格控件-GrIDVIEw
(4)下拉列表控件-Spinner
(5)自动提示文本框-autoCompleteTextVIEw
(6)支持展开/收缩功能的列表控件-ExpandableListVIEw
适配器的作用是用来处理数据并将数据绑定到AdapterVIEw上,是AdapterVIEw视图与与数据之间的一个桥梁。

/**  * @author: xiaolijuan  * @description: Spinner适配器  * @projectname: SpinnerProject  * @date: 2015-10-18  * @time: 00:19  */ public class SpinnerAdapter extends BaseAdapter {   private Context context;   private String[] array;   private int layoutID;    /**    * 构造方法    * @param context 上下文对象    * @param array 数组    * @param layoutID 布局ID    */   public SpinnerAdapter(Context context,String[] array,int layoutID) {     this.context = context;     this.array = array;     this.layoutID = layoutID;   }    /**    * 获取Item总数    * @return    */   @OverrIDe   public int getCount() {     return array.length;   }    /**    * 获取一个Item对象    * @param position    * @return    */   @OverrIDe   public Object getItem(int position) {     return array[position];   }    /**    * 获取指定item的ID    * @param position    * @return    */   @OverrIDe   public long getItemID(int position) {     return position;   }    /**    * 绘制的内容均在此实现    * @param position position就是位置从0开始    * @param convertVIEw convertVIEw是Spinner中每一项要显示的vIEw    * @param parent parent就是父窗体了,也就是Spinner    * @return    */   @OverrIDe   public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {     VIEw item = convertVIEw != null ? convertVIEw : VIEw.inflate(context,layoutID,null);     TextVIEw txt_name = (TextVIEw) item.findVIEwByID(R.ID.txt_name);     txt_name.setText(array[position]);     return item;   } } 

java 代码:注释写的很清楚

/**  * 使用Spinner实现城市级联下拉框  * Spinner最简单使用方式步骤如下:  * 第一步:在布局文件中添加Spinner控件。  * 第二步:在Acitvity中通过ID找到它。  * 第三步:给Spinner绑定一个适配器。  * 第四步:绑定监听器就可以用了。  */ public class MainActivity extends Activity {   private Spinner spinner1,spinner2,spinner3;    @OverrIDe   protected voID onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentVIEw(R.layout.activity_main);      spinner1 = (Spinner) findVIEwByID(R.ID.spinner1);     spinner2 = (Spinner) findVIEwByID(R.ID.spinner2);     spinner3 = (Spinner) findVIEwByID(R.ID.spinner3);      //加载省份列表     loadProvince();     //设置spinner1的监听事件     spinner1.setonItemSelectedListener(new Spinner1ClickListener());     //加载城市列表     loadCity();     //设置spinner2的监听事件     spinner2.setonItemSelectedListener(new Spinner2ClickListener());     //加载区域列表     loadGZArea();     //设置spinner3的监听事件     spinner3.setonItemSelectedListener(new Spinner3ClickListener());   }    /**    * 加载省份列表    */   public voID loadProvince() {     String[] array1 = new String[]{"请选择","广东省"};     SpinnerAdapter adapterOne = new SpinnerAdapter(this,array1,R.layout.activity_item);     spinner1.setAdapter(adapterOne);   }    /**    * 加载城市列表    */   public voID loadCity() {     String[] array2 = new String[]{"请选择","广州市","深圳市"};     SpinnerAdapter modelTwo = new SpinnerAdapter(this,array2,R.layout.activity_item);     spinner2.setAdapter(modelTwo);   }    /**    * 加载广州区域列表    */   public voID loadGZArea() {     String[] array3 = new String[]{"请选择","天河区","越秀区","荔湾区","海珠区","萝岗区","白云区","黄埔区","花都区"};     SpinnerAdapter modelThree = new SpinnerAdapter(this,array3,R.layout.activity_item);     spinner3.setAdapter(modelThree);   }    /**    * 加载深圳区域列表    */   public voID loadSZArea() {     String[] array3 = new String[]{"请选择","龙岗区","南山区","福田区","罗湖区","盐田区","宝安区"};     SpinnerAdapter modelThree = new SpinnerAdapter(this,R.layout.activity_item);     spinner3.setAdapter(modelThree);   }    /**    * Spinner1点击事件    */   public class Spinner1ClickListener implements AdapterVIEw.OnItemSelectedListener {      @OverrIDe     public voID onItemSelected(AdapterVIEw<?> adapterVIEw,VIEw vIEw,int i,long l) {       String str = (String) adapterVIEw.getItemAtposition(i);       //判断是否选择城市,如果没有选择那么就隐藏Spinner2,Spinner3两个下拉框,否则显示Spinner2下拉框,继续隐藏Spinner3       if (str.equals("请选择")) {         spinner2.setVisibility(VIEw.INVISIBLE);         spinner3.setVisibility(VIEw.INVISIBLE);       } else {         spinner2.setVisibility(VIEw.VISIBLE);          //将第二个下拉框的选项重新设置为选中“请选择”这个选项。         spinner2.setSelection(0);       }        Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();     }      @OverrIDe     public voID onnothingSelected(AdapterVIEw<?> adapterVIEw) {      }   }    /**    * Spinner2点击事件    */   public class Spinner2ClickListener implements AdapterVIEw.OnItemSelectedListener {      @OverrIDe     public voID onItemSelected(AdapterVIEw<?> adapterVIEw,long l) {       String str = (String) adapterVIEw.getItemAtposition(i);       if (str.equals("请选择")) {         spinner3.setVisibility(VIEw.INVISIBLE);       } else {         //显示第三个Spinner3         spinner3.setVisibility(VIEw.VISIBLE);          if (str.equals("深圳市")) {           //重新加载深圳区域列表           loadSZArea();         } else if (str.equals("广州市")) {           //重新加载广州区域列表           loadGZArea();         }       }       Toast.makeText(getApplicationContext(),Toast.LENGTH_SHORT).show();     }      @OverrIDe     public voID onnothingSelected(AdapterVIEw<?> adapterVIEw) {      }   }    /**    * Spinner3点击事件    */   public class Spinner3ClickListener implements AdapterVIEw.OnItemSelectedListener {      @OverrIDe     public voID onItemSelected(AdapterVIEw<?> adapterVIEw,long l) {       String str = (String) adapterVIEw.getItemAtposition(i);       Toast.makeText(getApplicationContext(),Toast.LENGTH_SHORT).show();     }      @OverrIDe     public voID onnothingSelected(AdapterVIEw<?> adapterVIEw) {      }   } } 

下面是布局的效果图

@H_419_41@

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android使用Spinner实现城市级联下拉框全部内容,希望文章能够帮你解决Android使用Spinner实现城市级联下拉框所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1143212.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存