android 地址控件概述

 

最近,公司做项目,需要一个地址控件,本来是想androidcopy开源的android的地址控件,但是了,找来找去。都没有找到一个真正满足我的需求的,普通的地址控件只是精确到市县区三级,但是我们的需求是精确到乡镇四级。于是,做了一个。

 我设计思路就是模仿jquery的并列的地址的选择控件,当选择上一级着,直接来跳转到下一级进行选择。设计的原型图为这样:

 

看到这样布局的方式很显然用gridview数据表格的方式来显示省市县的数据。而每个的数据网格的布局的方式只需要一个textview来显示相应的数据的方式就可以了。这两个的布局文件的样子如下所示:

gridview的布局文件的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <GridView
                android:id="@+id/gridview_county"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@color/back_color"
                android:gravity="center"
                android:horizontalSpacing="1dp"
                android:numColumns="5"
                
                android:columnWidth="80dp"
                android:stretchMode="columnWidth"
                android:verticalSpacing="1dp" />
</LinearLayout>

每项item的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <GridView
                android:id="@+id/gridview_county"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@color/back_color"
                android:gravity="center"
                android:horizontalSpacing="1dp"
                android:numColumns="5"
                
                android:columnWidth="80dp"
                android:stretchMode="columnWidth"
                android:verticalSpacing="1dp" />
</LinearLayout>

这两个文件都十分的简洁,真是有了这个布局文件作为基础,才能实现这个控件的真正的布局来实现了,那么他的真正的组装的布局的文件又是那个样子了。这就用了一个slideview来把这四个布局文件来包裹起来了。同时来实现相应的动画效果。他的布局文件如下:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/table"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:background="#FFFFFFFF"
        android:orientation="horizontal" >

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@drawable/a"
            android:clickable="true" >

            <TextView
                android:id="@+id/table_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="8dp"
                android:tag="0"
                android:text="省份"
                android:textColor="@color/text_down"
                android:textSize="16dp" />
        </FrameLayout>

        <FrameLayout
            android:id="@+id/frame_route_baseinfo"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@drawable/b"
            android:clickable="false"
            android:focusable="false" >

            <TextView
                android:id="@+id/table_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="8dp"
                android:tag="1"
                android:text="地市"
                android:textColor="@color/text_normal"
                android:textSize="16dp" />
        </FrameLayout>

        <FrameLayout
            android:id="@+id/frame_countyinfo"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@drawable/b"
            android:clickable="false"
            android:focusable="false"
            android:textColor="@color/text_normal" >

            <TextView
                android:id="@+id/table_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="8dp"
                android:tag="2"
                android:text="区县"
                android:textColor="@color/text_normal"
                android:textSize="16dp" />
        </FrameLayout>

        <FrameLayout
            android:id="@+id/frame_countryinfo"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@drawable/b"
            android:clickable="true" >

            <TextView
                android:id="@+id/table_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="8dp"
                android:tag="3"
                android:text="乡镇/街道"
                android:textColor="@color/text_normal"
                android:textSize="16dp" />
        </FrameLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/c"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <com.example.address_ctrl.element.SlideChangeView
                android:id="@+id/slide_change_page"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <LinearLayout
                    android:id="@+id/provinceview_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <include
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        layout="@layout/provinceview" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/cityview_layout"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <include
                        android:layout_width="match_parent"
                        android:layout_height="fill_parent"
                        layout="@layout/cityview" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/countyview_layout"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <include
                        android:layout_width="match_parent"
                        android:layout_height="fill_parent"
                        layout="@layout/countyview" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/countryview_layout"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <include
                        android:layout_width="match_parent"
                        android:layout_height="fill_parent"
                        layout="@layout/countryview" />
                </LinearLayout>
            </com.example.address_ctrl.element.SlideChangeView>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="5dp"
        android:paddingBottom="20dp" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.2"
                android:text="选择地址"
                android:textColor="@color/normal_color" />

            <EditText
                android:id="@+id/address_edit"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.6"
                android:background="@color/text_normal"
                android:editable="false"
                android:textColor="@color/black_color" />

            <Button
                android:id="@+id/car_no_btn"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.2"
                android:text="确定" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

通过上面的源代码,我们可以明确的看到,有个editext来接受你选择的相应的结果,有个button来确定你选择的结果,这样子的话,就构成 一个完整的地址的空间的布局了。

值得一提的,这里有一个所谓的SlideChangeView自定义的控件,这实现的功能本身和viewflipper差不多了,这不过这里能够继承了自定义的动画,似的翻页的时候来更生动形象。至于这里的控制文件无非是加载数据在进行了相应的逻辑判断,我就不再这里进行太多赘述。

最终控件的效果如下:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.省,市,区三级关联 2.地址数据本地化 3.自定义对话框 4.支付从xml文件,或对象文件加载 部分代码: package com.demo.selector; import java.io.InputStream; import com.address.selector.City; import com.address.selector.Country; import com.address.selector.Province; import com.address.selector.Region; import com.demo.address.R; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; import android.widget.AdapterView.OnItemSelectedListener; public class AddressSelectorDialog extends BaseDialog { /**省类型*/ private static final int INDEX_PROVINCE = 1; /**城市类型*/ private static final int INDEX_CITY = 2; /**区县类型*/ private static final int INDEX_REGION = 3; private static Country china = null; private Spinner spnProvince = null; private Spinner spnCity = null; private Spinner spnRegion = null; private ArrayAdapter dptProvince = null; private ArrayAdapter dptCity = null; private ArrayAdapter dptRegion = null; private SpinnerSelectedListener proListener = new SpinnerSelectedListener(); private SpinnerSelectedListener cityListener = new SpinnerSelectedListener(); private SpinnerSelectedListener regionListener = new SpinnerSelectedListener(); //当前选择地区的索引值 private int provinceId = -1; private int cityId = -1; private int regionId = -1; /**选择按钮*/ private Button btnSelect = null; /**回调用事件*/ private OnSelectorCancel selectorCancel = null; @Override public void init(Context context) { // TODO Auto-generated method stub this.context = context; dialog = new Dialog(this.context,R.style.CustomDialog); dialog.setContentView(R.layout.adress_selector); spnProvince = (Spinner)dialog.findViewById(R.id.spin_province); spnProvince.setTag(new Integer(INDEX_PROVINCE)); spnCity = (Spinner)dialog.findViewById(R.id.spin_city); spnCity.setTag(new Integer(INDEX_CITY)); spnRegion = (Spinner)dialog.findViewById(R.id.spin_region); spnRegion.setTag(new Integer(INDEX_REGION)); btnSelect = (Button)dialog.findViewById(R.id.btn_select); btnSelect.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub selectAddress(); } }); //当对话框隐藏后调用[回调事件] dialog.setOnCancelListener(new OnCancelListener(){ @Override public void onCancel(DialogInterface arg0) { // TODO Auto-generated method stub if (selectorCancel != null) { String proName = ""; String cityName = ""; String regName = ""; try { Province tmpPro = china.provinceLst.get(provinceId); proName = tmpPro.areaName; City tmpCity = tmpPro.cityLst.get(cityId); cityName = tmpCity.areaName; Region tmpReg = tmpCity.regionLst.get(regionId); regName = tmpReg.areaName; } catch (Exception e) { android.util.Log.e("selectorCancel???", e.getMessage()); } selectorCancel.onSelectorResult(proName, cityName, regName); } } }); //加载数据 if (china == null) { loadCountryAddressData(); } //加载完后,初始化适配器 initAdapters(); } @Override public void update(Object obj) { // TODO Auto-generated method stub } private void loadCountryAddressData() { try { /** * 反序化的包名,类名,版本ID必须一致,否则ClassNotFoundException */ InputStream stream = this.context.getAssets().open("china-city.obj"); china = Country.loadFromStream(stream); //mHandler.sendEmptyMessage(0); } catch (Exception e) { android.util.Log.e("error", e.getMessage()); } } class SpinnerSelectedListener implements OnItemSelectedListener{ @Override public void onItemSelected (AdapterView parent, View view, int position, long id) { // TODO Auto-generated method stub //android.util.Log.i("", String.format("[pos=%d]",position)); int index = (Integer)parent.getTag(); //ArrayAdapter obj = (ArrayAdapter)(parent.getAdapter()); //String value = obj.getItem(position); //android.util.Log.i("", "value=" + value); if (index == INDEX_PROVINCE) { provinceId = position; if (isValideProvinceIndex(provinceId)) { //改变城市选择 Province tmpProvince = china.provinceLst.get(provinceId); String lstNames[] = tmpProvince.listNames(); if (lstNames == null) { //没有城市,当然没区县 provinceId = -1; spnCity.setVisibility(View.INVISIBLE); regionId = -1; spnRegion.setVisibility(View.INVISIBLE); return ; } else { if (spnCity.getVisibility() == View.INVISIBLE) { spnCity.setVisibility(View.VISIBLE); } if (spnRegion.getVisibility() == View.INVISIBLE) { spnRegion.setVisibility(View.INVISIBLE); } } dptCity = new ArrayAdapter(context, android.R.layout.simple_spinner_item, lstNames); dptCity.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spnCity.setAdapter(dptCity); spnCity.setOnItemSelectedListener(cityListener); } } else if (index == INDEX_CITY) { cityId = position; if (isValideCityIndex(provinceId,cityId)) { //改变区选择 Province tmpProvince = china.provinceLst.get(provinceId); City tmpCity = tmpProvince.cityLst.get(cityId); String lstNames[] = tmpCity.listNames(); if (lstNames == null) { //没有区县 regionId = -1; spnRegion.setVisibility(View.INVISIBLE); return ; } else if (spnRegion.getVisibility() == View.INVISIBLE) { spnRegion.setVisibility(View.VISIBLE); } dptRegion = new ArrayAdapter(context, android.R.layout.simple_spinner_item, lstNames); dptRegion.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spnRegion.setAdapter(dptRegion); spnRegion.setOnItemSelectedListener(regionListener); } } else if (index == INDEX_REGION) { regionId = position; } } @Override public void onNothingSelected(AdapterView parent) { // TODO Auto-generated method stub } } private void initAdapters() { dptProvince = new ArrayAdapter(this.context, android.R.layout.simple_spinner_item, china.listNames()); //设置下拉列表的风格 dptProvince.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //设置适配器 spnProvince.setAdapter(dptProvince); //设置侦听事件 spnProvince.setOnItemSelectedListener(proListener); } private boolean isValideProvinceIndex(int index) { return (china != null) && (index >= 0 && index = 0) && (china.provinceLst.get(provinceIndex).cityLst.size() > cityIndex); } return false; } private void selectAddress() { hide(); } /** * 调协回调事件 * @param selectorCancel */ public void setOnSelectorCancel(OnSelectorCancel selectorCancel) { this.selectorCancel = selectorCancel; } /** * 选择器关闭后,调用回调事件接口 */ public interface OnSelectorCancel { /** * @param proName 省份名 * @param cityName 城市名 * @param regName 区县名 */ public void onSelectorResult(String proName,String cityName,String regName); }; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值