自定义弹窗(三) 弹窗选择三级联动城市

上一期 自定义弹窗(二)讲了监听自定义弹窗的按钮。

这一期做一个自定义选择联动城市的自定义弹窗。

效果图


核心代码如下:

alertdialog_sel_city.xml

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

    <RelativeLayout
        android:id="@+id/AlertDialogTop_Layout"
        android:layout_width="500dp"
        android:layout_height="wrap_content"
        android:background="@drawable/alertdialogtop_bg" >

        <!-- 弹窗Title -->

        <TextView
            android:id="@+id/AlertDialogTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"
            android:textColor="@color/skyblue"
            android:textSize="24sp" />

        <Button
            android:id="@+id/btn_close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/alertdialog_close" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/AlertDialogContent_Layout"
        android:layout_width="500dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/AlertDialogTop_Layout"
        android:background="@drawable/alertdialog_bg" >

        <RelativeLayout
            android:id="@+id/add_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp" >

            <RelativeLayout
                android:id="@+id/add_province_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/add_province_txt"
                    android:layout_width="100dp"
                    android:layout_height="50dp"
                    android:gravity="center|right"
                    android:hint="@string/add_province"
                    android:textColor="@color/black"
                    android:textSize="20sp" />

                <Spinner
                    android:id="@+id/add_province_box"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_toRightOf="@+id/add_province_txt"
                    android:textColor="@color/black"
                    android:textSize="20sp" >
                </Spinner>
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/add_city_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/add_province_layout"
                android:layout_marginTop="10dp" >

                <TextView
                    android:id="@+id/add_city_txt"
                    android:layout_width="100dp"
                    android:layout_height="50dp"
                    android:gravity="center|right"
                    android:hint="@string/add_city"
                    android:textColor="@color/black"
                    android:textSize="20sp" />

                <Spinner
                    android:id="@+id/add_city_box"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_toRightOf="@+id/add_city_txt"
                    android:textColor="@color/black"
                    android:textSize="20sp" >
                </Spinner>
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/add_street_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/add_city_layout"
                android:layout_marginTop="10dp" >

                <TextView
                    android:id="@+id/add_street_txt"
                    android:layout_width="100dp"
                    android:layout_height="50dp"
                    android:gravity="center|right"
                    android:hint="@string/add_street"
                    android:textColor="@color/black"
                    android:textSize="20sp" />

                <Spinner
                    android:id="@+id/add_street_box"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_toRightOf="@+id/add_street_txt"
                    android:textColor="@color/black"
                    android:textSize="20sp" >
                </Spinner>
            </RelativeLayout>
        </RelativeLayout>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/AlertDialogEnd_Layout"
        android:layout_width="500dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/AlertDialogContent_Layout"
        android:background="@drawable/alertdialogend_bg" >

        <LinearLayout
            android:id="@+id/AlertDialogBtn_Layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_margin="5dp" >

            <LinearLayout
                android:id="@+id/ok_btn_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/alertdialog_btn_bg" >

                <TextView
                    android:id="@+id/ok_btn_txt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_margin="5dp"
                    android:text="确定"
                    android:textColor="@color/black"
                    android:textSize="20sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/cancle_btn_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:background="@drawable/alertdialog_btn_bg" >

                <TextView
                    android:id="@+id/cancle_btn_txt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_margin="5dp"
                    android:text="取消"
                    android:textColor="@color/black"
                    android:textSize="20sp" />
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>

</RelativeLayout>

Main.java

package com.test.jooylife.myalert3;

import java.util.ArrayList;
import com.test.jooylife.myalert3.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class Main extends Activity
{
	/** 微软雅黑默认 */
	private static Typeface typefacenormal = Typeface.create("微软雅黑", Typeface.NORMAL);
	/** 微软雅黑粗体 */
	private static Typeface typefacebold = Typeface.create("微软雅黑", Typeface.BOLD);

	private AlertDialog add_addressdlg;
	private String selcityid = "";
	private ArrayList<WeatherAreaModel> weatherAreaModels;
	private WeatherAreaModel nowWeatherAreaModel;
	private Spinner add_province_box;
	private ArrayList<WeatherAreaModel> provinceList = new ArrayList<WeatherAreaModel>();
	private ArrayAdapter<String> provinceAdapter;
	private Spinner add_city_box;
	private ArrayList<WeatherAreaModel> cityList = new ArrayList<WeatherAreaModel>();
	private ArrayAdapter<String> cityAdapter;
	private Spinner add_street_box;
	private ArrayList<WeatherAreaModel> streetList = new ArrayList<WeatherAreaModel>();
	private ArrayAdapter<String> streetAdapter;
	private Button add_address_close;
	private LinearLayout add_address_ok;
	private LinearLayout add_address_cancle;

	private TextView message_txt;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		weatherAreaModels = WeatherAreaList.GetList();
		selcityid = "101010100";
		GetNowWeatherArea(selcityid);

		Button clickbtn = (Button) this.findViewById(R.id.clickbtn);

		message_txt = (TextView) this.findViewById(R.id.message_txt);

		clickbtn.setOnClickListener(listener);
	}

	private OnClickListener listener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			switch (v.getId())
			{
			case R.id.clickbtn:
				CreateAddress();
				break;
			}
		}
	};

	/**
	 * 创建天气城市弹窗
	 * 
	 */
	private void CreateAddress()
	{
		if (add_addressdlg != null)
		{
			if (add_addressdlg.isShowing())
			{
				add_addressdlg.dismiss();
			}
			add_addressdlg = null;
		}
		View add_addressdlgView;
		LayoutInflater factory = LayoutInflater.from(this);
		add_addressdlg = new AlertDialog.Builder(this).setCancelable(false).create();
		add_addressdlgView = factory.inflate(R.layout.alertdialog_sel_city, null);
		add_addressdlg.setView(add_addressdlgView);
		add_addressdlg.show();
		Window window = add_addressdlg.getWindow();
		add_addressdlg.setContentView(R.layout.alertdialog_sel_city);
		TextView title = (TextView) window.findViewById(R.id.AlertDialogTitle);
		title.setTypeface(typefacenormal);
		title.setText("选择天气预报城市");

		TextView add_province_txt = (TextView) window.findViewById(R.id.add_province_txt);
		add_province_box = (Spinner) window.findViewById(R.id.add_province_box);
		add_province_txt.setTypeface(typefacenormal);
		GetProvinceList();

		if (nowWeatherAreaModel != null)
		{
			BindProvince(nowWeatherAreaModel.areaname1());
		} else
		{
			BindProvince(null);
		}

		add_province_box.setOnItemSelectedListener(new OnItemSelectedListener()
		{
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
			{
				String name = (String) add_province_box.getSelectedItem();
				WeatherAreaModel model = GetProvinceByName(name);
				GetCityList(model.areaname1());
				if (nowWeatherAreaModel != null)
				{
					BindCity(nowWeatherAreaModel.areaname2());
				} else
				{
					BindCity(null);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0)
			{
				// TODO Auto-generated method stub

			}
		});

		TextView add_city_txt = (TextView) window.findViewById(R.id.add_city_txt);
		add_city_box = (Spinner) window.findViewById(R.id.add_city_box);
		add_city_txt.setTypeface(typefacenormal);

		add_city_box.setOnItemSelectedListener(new OnItemSelectedListener()
		{
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
			{
				String name = (String) add_city_box.getSelectedItem();
				WeatherAreaModel model = GetCityByName(name);
				GetStreetList(model.areaname2());
				if (nowWeatherAreaModel != null)
				{
					BindStreet(nowWeatherAreaModel.areaname3());
				} else
				{
					BindStreet(null);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0)
			{
				// TODO Auto-generated method stub

			}
		});

		TextView add_street_txt = (TextView) window.findViewById(R.id.add_street_txt);
		add_street_box = (Spinner) window.findViewById(R.id.add_street_box);
		add_street_txt.setTypeface(typefacenormal);

		add_street_box.setOnItemSelectedListener(new OnItemSelectedListener()
		{
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
			{
				String name = (String) add_street_box.getSelectedItem();
				WeatherAreaModel model = GetStreetByName(name);
				selcityid = model.areaid();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0)
			{
				// TODO Auto-generated method stub

			}
		});

		add_address_close = (Button) window.findViewById(R.id.btn_close);

		add_address_ok = (LinearLayout) window.findViewById(R.id.ok_btn_layout);
		TextView ok_btn_txt = (TextView) window.findViewById(R.id.ok_btn_txt);
		ok_btn_txt.setTypeface(typefacebold);

		add_address_cancle = (LinearLayout) window.findViewById(R.id.cancle_btn_layout);
		TextView cancle_btn_txt = (TextView) window.findViewById(R.id.cancle_btn_txt);
		cancle_btn_txt.setTypeface(typefacebold);

		add_address_close.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				add_addressdlg.dismiss();
			}
		});

		add_address_cancle.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				add_addressdlg.dismiss();
			}
		});

		add_address_ok.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				GetNowWeatherArea(selcityid);
				add_addressdlg.dismiss();
			}
		});
	}

	/**
	 * 当前天气城市
	 * 
	 * @param ctiyid
	 */
	private void GetNowWeatherArea(String areaid)
	{
		for (WeatherAreaModel item : weatherAreaModels)
		{
			if (item.areaid().equals(areaid))
			{
				nowWeatherAreaModel = item;
				break;
			}
		}
		if (nowWeatherAreaModel == null)
		{
			nowWeatherAreaModel = weatherAreaModels.get(0);
		}

		Message msg = new Message();
		msg.what = 0;
		msg.obj = "当前选择城市:id:" + nowWeatherAreaModel.areaid() + "," + nowWeatherAreaModel.areaname1() + "-" + nowWeatherAreaModel.areaname2() + "-" + nowWeatherAreaModel.areaname3();
		messageHandler.sendMessage(msg);
	}

	/**
	 * 根据名称获取一级城市
	 * 
	 * @param name
	 * @return
	 */
	private WeatherAreaModel GetProvinceByName(String name)
	{
		WeatherAreaModel model = null;
		for (WeatherAreaModel item : provinceList)
		{
			if (item.areaname1().equals(name))
			{
				model = item;
				break;
			}
		}
		return model;
	}

	/**
	 * 根据名称获取二级城市
	 * 
	 * @param name
	 * @return
	 */
	private WeatherAreaModel GetCityByName(String name)
	{
		WeatherAreaModel model = null;
		for (WeatherAreaModel item : cityList)
		{
			if (item.areaname2().equals(name))
			{
				model = item;
				break;
			}
		}
		return model;
	}

	/**
	 * 根据名称获取三级城市
	 * 
	 * @param name
	 * @return
	 */
	private WeatherAreaModel GetStreetByName(String name)
	{
		WeatherAreaModel model = null;
		for (WeatherAreaModel item : streetList)
		{
			if (item.areaname3().equals(name))
			{
				model = item;
				break;
			}
		}
		return model;
	}

	/**
	 * 获取一级城市列表
	 */
	private void GetProvinceList()
	{
		provinceList = new ArrayList<WeatherAreaModel>();
		for (WeatherAreaModel item : weatherAreaModels)
		{
			if (!checkProvince(item.areaname1()))
			{
				provinceList.add(item);
			}
		}
	}

	/**
	 * 绑定省
	 * 
	 * @param provincename
	 */
	private void BindProvince(String provincename)
	{
		ArrayList<String> list = new ArrayList<String>();
		for (WeatherAreaModel item : provinceList)
		{
			list.add(item.areaname1());
		}
		provinceAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
		provinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		add_province_box.setAdapter(provinceAdapter);
		if (provincename == null)
		{
			add_province_box.setSelection(0);
		} else
		{
			for (int i = 0; i < provinceList.size(); i++)
			{
				if (provincename.equals(provinceList.get(i).areaname1()))
				{
					add_province_box.setSelection(i);
					break;
				}
			}
		}
	}

	/**
	 * 是否已存在该一级城市
	 * 
	 * @param name
	 * @return
	 */
	private boolean checkProvince(String name)
	{
		boolean t = false;
		for (WeatherAreaModel item : provinceList)
		{
			if (item.areaname1().equals(name))
			{
				t = true;
				break;
			}
		}
		return t;
	}

	/**
	 * 获取二级城市
	 * 
	 * @param pname
	 */
	private void GetCityList(String pname)
	{
		cityList = new ArrayList<WeatherAreaModel>();
		for (WeatherAreaModel item : weatherAreaModels)
		{
			if (item.areaname1().equals(pname))
			{
				if (!checkCity(item.areaname2()))
				{
					cityList.add(item);
				}
			}
		}
	}

	/**
	 * 绑定市
	 * 
	 * @param cityname
	 */
	private void BindCity(String cityname)
	{
		ArrayList<String> list = new ArrayList<String>();
		for (WeatherAreaModel item : cityList)
		{
			list.add(item.areaname2());
		}
		cityAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
		cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		add_city_box.setAdapter(cityAdapter);
		if (cityname == null)
		{
			add_city_box.setSelection(0);
		} else
		{
			for (int i = 0; i < cityList.size(); i++)
			{
				if (cityname.equals(cityList.get(i).areaname2()))
				{
					add_city_box.setSelection(i);
					break;
				}
			}
		}
	}

	/**
	 * 是否已存在该二级城市
	 * 
	 * @param name
	 * @return
	 */
	private boolean checkCity(String name)
	{
		boolean t = false;
		for (WeatherAreaModel item : cityList)
		{
			if (item.areaname2().equals(name))
			{
				t = true;
				break;
			}
		}
		return t;
	}

	/**
	 * 获取三级城市
	 * 
	 * @param pname
	 */
	private void GetStreetList(String pname)
	{
		streetList = new ArrayList<WeatherAreaModel>();
		for (WeatherAreaModel item : weatherAreaModels)
		{
			if (item.areaname2().equals(pname))
			{
				if (!checkStreet(item.areaname3()))
				{
					streetList.add(item);
				}
			}
		}
	}

	/**
	 * 绑定区
	 * 
	 * @param streetname
	 */
	private void BindStreet(String streetname)
	{
		ArrayList<String> list = new ArrayList<String>();
		for (WeatherAreaModel item : streetList)
		{
			list.add(item.areaname3());
		}
		streetAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
		streetAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		add_street_box.setAdapter(streetAdapter);
		if (streetname == null)
		{
			add_street_box.setSelection(0);
		} else
		{
			for (int i = 0; i < streetList.size(); i++)
			{
				if (streetname.equals(streetList.get(i).areaname3()))
				{
					add_street_box.setSelection(i);
					break;
				}
			}
		}
	}

	/**
	 * 是否已存在该三级城市
	 * 
	 * @param name
	 * @return
	 */
	private boolean checkStreet(String name)
	{
		boolean t = false;
		for (WeatherAreaModel item : streetList)
		{
			if (item.areaname3().equals(name))
			{
				t = true;
				break;
			}
		}
		return t;
	}

	private Handler messageHandler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
			case 0:
				message_txt.setText(msg.obj + "");
				break;
			}
		}
	};
}

本例代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值