仿淘宝收货地址之三级联动

本文介绍如何实现省市区三级联动,针对项目中收货地址的需求,分享作者整理的代码,避免网上不实资源,提供实际可用的解决方案,并附带布局文件和部分关键代码展示。

仿淘宝收货地址之三级联动,以前做项目有这个需求在网上找了好久都没有真正的满足要求的,

大部分都是骗取下载积分的,所以还是自己整理了以下,希望对大家有帮助,给那些受骗的人一些安慰吧。


话不多说先上效果图:






布局文件代码如下:

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

    android:orientation="vertical"
    android:background="@android:color/holo_red_light"
    android:layout_width="match_parent"
    android:layout_height="55dp">

    <TextView
        android:gravity="center"
        android:textColor="#ffffff"
        android:textSize="22dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="收货地址"/>
</LinearLayout>
     <LinearLayout
android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        
        <TextView
        android:padding="10dp"
        android:textSize="16sp"
        android:background="#ffffff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="收货人:"
        android:layout_alignParentLeft="true"
            />
    <EditText
        android:layout_weight="1"
        android:id="@+id/et_shouhuoren"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:textSize="16sp"
        android:padding="10dp" />
</LinearLayout>
    
    <View 
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#F6F5F4"
        />
      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        
        <TextView 
        
        android:padding="10dp"
        android:textSize="16sp"
        android:background="#ffffff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="手机号:"
        android:layout_alignParentLeft="true"
            />
    <EditText
        android:layout_weight="1"
        android:id="@+id/et_Mobile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:textSize="16sp"
        android:padding="10dp" />
</LinearLayout>
    
    <View 
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#F6F5F4"
        />
      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        
        <TextView 
        
        android:padding="10dp"
        android:textSize="16sp"
        android:background="#ffffff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="邮编    :"
        android:layout_alignParentLeft="true"
            />
    <EditText
        android:layout_weight="1"
        android:id="@+id/et_youbian"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:textSize="16sp"
        android:padding="10dp" />
</LinearLayout>
    
    <View 
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#F6F5F4"
        />
    <LinearLayout
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        
        <TextView 
        
        android:padding="10dp"
        android:textSize="16sp"
        android:background="#ffffff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="所在地区:"
        android:layout_alignParentLeft="true"
            />
        
         <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
    <TextView
        android:layout_weight="1"
        android:clickable="true"
        android:id="@+id/tv_city1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:padding="10dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/icon_right_jiantou" />

  </RelativeLayout>
  
</LinearLayout>

    <View 
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#F6F5F4"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        
        <TextView 
        
        android:padding="10dp"
        android:textSize="16sp"
        android:background="#ffffff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="详细地址:"
        android:layout_alignParentLeft="true"
            />
    <EditText 
        android:layout_weight="1"
        android:id="@+id/et_xiangxidizhi"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:textSize="16sp"
        android:padding="10dp" />
</LinearLayout>
    
    <View 
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#F6F5F4"
        />
    

    
    <Button
        android:id="@+id/btn_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@android:color/holo_red_light"
        android:text="确定"
        android:textColor="@color/white"
        android:textSize="16sp" />

</LinearLayout>

部分代码:

package com.an.cityselect;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 多城市选择类
 *
 *2016-9
 * @author zander___東
 *
 */
public class CitySelect2Activity extends Activity implements OnClickListener {

   private Button btn_back, btn_right;
   private ListView lv_city;
   private ArrayList<MyRegion> regions;

   private CityAdapter adapter;
   private static int PROVINCE = 0x00;
   private static int CITY = 0x01;
   private static int DISTRICT = 0x02;
   private CityUtils util;

   /**头部省市区三个按钮*/
   private TextView[] tvs = new TextView[3];
   //private int[] ids = { R.id.rb_province, R.id.rb_city, R.id.rb_district };
   private int[] ids;
   private City city;
   /**添加的(多个)城市*/
   private ArrayList<City> toCitys;
   private LinearLayout ll_add;
   int last, current;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.activity_city2);

      viewInit();

   }

   private void viewInit() {

      city = new City();
      Intent in = getIntent();
      toCitys = in.getParcelableArrayListExtra("toCitys");

      if (toCitys == null)
         toCitys = new ArrayList<City>();



      for (int i = 0; i < tvs.length; i++) {
         tvs[i] = (TextView) findViewById(ids[i]);
         tvs[i].setOnClickListener(this);
      }

      btn_back = (Button) findViewById(R.id.btn_back);
      btn_right = (Button) findViewById(R.id.btn_right);
      btn_right.setText("添加");
      ll_add = (LinearLayout) findViewById(R.id.ll_add);
      util = new CityUtils(this, hand);
      util.initProvince();
      tvs[current].setBackgroundColor(0xff999999);
      lv_city = (ListView) findViewById(R.id.lv_city);

      regions = new ArrayList<MyRegion>();
      adapter = new CityAdapter(this);
      lv_city.setAdapter(adapter);


      for (int i = 0; i < toCitys.size(); i++) {
         TextView tv = new TextView(this);
         LayoutParams params = new LayoutParams(100,LayoutParams.MATCH_PARENT);
         tv.setLayoutParams(params);
         tv.setText(toCitys.get(i).getCity());
         tv.setTextColor(Color.RED);
         ll_add.addView(tv);
      }
   }

   protected void onStart() {
      super.onStart();
      lv_city.setOnItemClickListener(onItemClickListener);
      btn_back.setOnClickListener(this);
      btn_right.setOnClickListener(this);
   };


   @SuppressLint("HandlerLeak")
   Handler hand = new Handler() {
      @SuppressWarnings("unchecked")
      public void handleMessage(android.os.Message msg) {

         switch (msg.what) {

            case 1:
               System.out.println("what======" + msg.what);

               regions = (ArrayList<MyRegion>) msg.obj;
               adapter.clear();
               adapter.addAll(regions);
               adapter.update();
               break;

            case 2:
               System.out.println("what======" + msg.what);
               regions = (ArrayList<MyRegion>) msg.obj;
               adapter.clear();
               adapter.addAll(regions);
               adapter.update();
               break;

            case 3:
               System.out.println("what======" + msg.what);
               regions = (ArrayList<MyRegion>) msg.obj;
               adapter.clear();
               adapter.addAll(regions);
               adapter.update();
               break;
         }
      };
   };

   OnItemClickListener onItemClickListener = new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {

         if (current == PROVINCE) {
            String newProvince = regions.get(arg2).getName();
            if (!newProvince.equals(city.getProvince())) {
               city.setProvince(newProvince);
               tvs[0].setText(regions.get(arg2).getName());
               city.setRegionId(regions.get(arg2).getId());
               city.setProvinceCode(regions.get(arg2).getId());
               city.setCityCode("");
               city.setDistrictCode("");
               tvs[1].setText("市");
               tvs[2].setText("区 ");
            }
            current = 1;

            util.initCities(city.getProvinceCode());
         } else if (current == CITY) {
            String newCity = regions.get(arg2).getName();
            if (!newCity.equals(city.getCity())) {
               city.setCity(newCity);
               tvs[1].setText(regions.get(arg2).getName());
               city.setRegionId(regions.get(arg2).getId());
               city.setCityCode(regions.get(arg2).getId());
               city.setDistrictCode("");
               tvs[2].setText("区 ");
            }
            util.initDistricts(city.getCityCode());
            current = 2;

         } else if (current == DISTRICT) {
            current = 2;
            city.setDistrictCode(regions.get(arg2).getId());
            city.setRegionId(regions.get(arg2).getId());
            city.setDistrict(regions.get(arg2).getName());
            tvs[2].setText(regions.get(arg2).getName());

         }
         tvs[last].setBackgroundColor(Color.WHITE);
         tvs[current].setBackgroundColor(Color.GRAY);
         last = current;
      }
   };

   //

   class CityAdapter extends ArrayAdapter<MyRegion> {

      LayoutInflater inflater;

      public CityAdapter(Context con) {
         super(con, 0);
         inflater = LayoutInflater.from(CitySelect2Activity.this);
      }

      @Override
      public View getView(int arg0, View v, ViewGroup arg2) {
         v = inflater.inflate(R.layout.city_item, null);
         TextView tv_city = (TextView) v.findViewById(R.id.tv_city);
         tv_city.setText(getItem(arg0).getName());
         return v;
      }

      public void update() {
         this.notifyDataSetChanged();
      }
   }

   @Override
   public void onClick(View v) {
      switch (v.getId()) {
         case R.id.btn_back:
            finish();
            break;
         case R.id.btn_right:

            if(city!=null&&city.getRegionId()!=null&&city.getCity()!=null){
               TextView tv = new TextView(this);
               LayoutParams params = new LayoutParams(100,LayoutParams.MATCH_PARENT);
               tv.setLayoutParams(params);
               tv.setText(city.getCity());
               tv.setTextColor(Color.RED);
               toCitys.add(city);
               ll_add.addView(tv);
               city = new City();
               current = 0;
               tvs[last].setBackgroundColor(Color.WHITE);
               tvs[current].setBackgroundColor(Color.GRAY);
               last = current;
               tvs[0].setText("省");
               tvs[1].setText("市");
               tvs[2].setText("区/县");
               util.initProvince();
            }
            break;

      }

         current = 2;
         util.initDistricts(city.getCityCode());
         tvs[last].setBackgroundColor(Color.WHITE);
         tvs[current].setBackgroundColor(Color.GRAY);
         last = current;
      

   }

}

好了,效果就是这样。http://download.csdn.net/detail/zdd199401/9637153


评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值