本组件主要用到了一个开源项目 Android Wheel Control 和 android的popWindow弹窗技术;

实现效果如下图:



不说废话,直接上代码

核心处理代码:

package com.example.poptest;import kankan.wheel.widget.OnWheelChangedListener;import kankan.wheel.widget.OnWheelScrollListener;import kankan.wheel.widget.WheelView;import kankan.wheel.widget.adapters.AbstractWheelTextAdapter;import kankan.wheel.widget.adapters.ArrayWheelAdapter; import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.view.Display;import android.view.Gravity;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.LinearLayout;import android.widget.PopupWindow;import android.widget.RelativeLayout;import android.widget.TextView;/****    * @production  packview风格的三级城市底部弹出菜单组件   * @company     android技术帮--【技术成就梦想】   * @department  群号:85506951   * @author      大灰狼叔叔(qq:953486326)   * {@docRoot    利用Android Wheel Control 实现的packview风格的三级城市底部弹出菜单 }   * @version     1.0   * @since       2013\4\15   * @see         欢迎对android感兴趣的童鞋加入android技术帮,共同学习,共同进步!   * ****/  public class MainActivity extends Activity {private static final String TAG = "MainActivity";private Button button,button_ok;RelativeLayout test_pop_layout;int width,height;private TextView tt ;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);  // 获取屏幕的高度和宽度Display display = this.getWindowManager().getDefaultDisplay();width = display.getWidth();    height = display.getHeight();    // 获取弹出的layout    test_pop_layout = (RelativeLayout)findViewById(R.id.test_pop_layout);    tt = (TextView)findViewById(R.id.tpop2_tv);      button = (Button) findViewById(R.id.button);button.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){// 显示 popupWindowPopupWindow popupWindow = makePopupWindow(MainActivity.this);int[] xy = new int[2];test_pop_layout.getLocationOnScreen(xy);popupWindow.showAtLocation(test_pop_layout,Gravity.CENTER|Gravity.BOTTOM, 0, -height); }});}  // Scrolling flag    private boolean scrolling = false;     private TextView tv;// 创建一个包含自定义view的PopupWindowprivate PopupWindow makePopupWindow(Context cx){final PopupWindow window; window = new PopupWindow(cx);           View contentView = LayoutInflater.from(this).inflate(R.layout.cities_layout, null);        window.setContentView(contentView);                        tv = (TextView)contentView.findViewById(R.id.tv_cityName);                final WheelView country = (WheelView) contentView.findViewById(R.id.country);        country.setVisibleItems(3);        country.setViewAdapter(new CountryAdapter(this));        final String cities[][] = AddressData.CITIES;         final String ccities[][][] = AddressData.COUNTIES;          final WheelView city = (WheelView) contentView.findViewById(R.id.city);        city.setVisibleItems(0);        country.addChangingListener(new OnWheelChangedListener() {public void onChanged(WheelView wheel, int oldValue, int newValue) {    if (!scrolling) {        updateCities(city, cities, newValue);    }}});                country.addScrollingListener( new OnWheelScrollListener() {            public void onScrollingStarted(WheelView wheel) {                scrolling = true;            }            public void onScrollingFinished(WheelView wheel) {                scrolling = false;                updateCities(city, cities, country.getCurrentItem());                                tv.setText( AddressData.PROVINCES[country.getCurrentItem()] );             }        });                // 地区选择        final WheelView ccity = (WheelView) contentView.findViewById(R.id.ccity);        ccity.setVisibleItems(0);        city.addChangingListener(new OnWheelChangedListener() {public void onChanged(WheelView wheel, int oldValue, int newValue) {    if (!scrolling) {        updatecCities(ccity, ccities, country.getCurrentItem(),newValue);     }}});                city.addScrollingListener( new OnWheelScrollListener() {            public void onScrollingStarted(WheelView wheel) {                scrolling = true;            }            public void onScrollingFinished(WheelView wheel) {                scrolling = false;                updatecCities(ccity, ccities, country.getCurrentItem(), city.getCurrentItem());                                tv.setText(               AddressData.PROVINCES[country.getCurrentItem()] + "-" +                AddressData.CITIES[country.getCurrentItem()][city.getCurrentItem()]);                           }        });                  ccity.addScrollingListener( new OnWheelScrollListener() {            public void onScrollingStarted(WheelView wheel) {                scrolling = true;            }            public void onScrollingFinished(WheelView wheel) {                scrolling = false;                                tv.setText(               AddressData.PROVINCES[country.getCurrentItem()] + "-" +                AddressData.CITIES[country.getCurrentItem()][city.getCurrentItem()] + "-" +                AddressData.COUNTIES[country.getCurrentItem()][city.getCurrentItem()][ccity.getCurrentItem()]);                            }        });                  country.setCurrentItem(1);                // 点击事件处理        button_ok = (Button)contentView.findViewById(R.id.button_ok);    button_ok.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){ tt.setText(AddressData.PROVINCES[country.getCurrentItem()] + "-" +                AddressData.CITIES[country.getCurrentItem()][city.getCurrentItem()] + "-" +                AddressData.COUNTIES[country.getCurrentItem()][city.getCurrentItem()][ccity.getCurrentItem()]);window.dismiss(); // 隐藏}});                  window.setWidth(width); window.setHeight(height/2);        // 设置PopupWindow外部区域是否可触摸window.setFocusable(true); //设置PopupWindow可获得焦点window.setTouchable(true); //设置PopupWindow可触摸window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸return window;} /**     * Updates the city wheel     */    private void updateCities(WheelView city, String cities[][], int index) {        ArrayWheelAdapter<String> adapter =            new ArrayWheelAdapter<String>(this, cities[index]);        adapter.setTextSize(18);        city.setViewAdapter(adapter);        city.setCurrentItem(cities[index].length / 2);        }        /**     * Updates the ccity wheel     */    private void updatecCities(WheelView city, String ccities[][][], int index,int index2) {        ArrayWheelAdapter<String> adapter =            new ArrayWheelAdapter<String>(this, ccities[index][index2]);        adapter.setTextSize(18);        city.setViewAdapter(adapter);        city.setCurrentItem(ccities[index][index2].length / 2);         }        /**     * Adapter for countries     */    private class CountryAdapter extends AbstractWheelTextAdapter {        // Countries names        private String countries[] = AddressData.PROVINCES;          /**         * Constructor         */        protected CountryAdapter(Context context) {            super(context, R.layout.country_layout, NO_RESOURCE);                        setItemTextResource(R.id.country_name);        }        @Override        public View getItem(int index, View cachedView, ViewGroup parent) {            View view = super.getItem(index, cachedView, parent);             return view;        }                @Override        public int getItemsCount() {            return countries.length;        }                @Override        protected CharSequence getItemText(int index) {            return countries[index];        }    }}

核心布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="fill_parent"android:orientation="vertical"android:background="@drawable/layout_bg"android:layout_width="fill_parent">    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:id="@+id/tv_cityName"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginTop="5dp"            android:layout_marginLeft="5dp"            android:text="请选择城市"              android:layout_weight="2"/>        <Button            android:id="@+id/button_ok"            android:layout_width="12dip"            android:layout_height="wrap_content"            android:layout_gravity="right"            android:layout_marginTop="5dp"            android:layout_marginRight="5dp"            android:text="确定"              android:layout_weight="1"/>    </LinearLayout><LinearLayout android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_width="fill_parent"android:paddingLeft="12dp"android:paddingRight="12dp"android:paddingTop="4dp"android:layout_marginTop="8dp"android:orientation="horizontal">   <kankan.wheel.widget.WheelView android:id="@+id/country"        android:layout_height="110dp"        android:layout_width="wrap_content"        android:layout_gravity="center_vertical"        android:layout_weight="1"/><kankan.wheel.widget.WheelView android:id="@+id/city"android:layout_height="110dp"android:layout_width="wrap_content"        android:layout_gravity="center_vertical"android:layout_marginLeft="5dp"android:layout_weight="1"/><kankan.wheel.widget.WheelView android:id="@+id/ccity"android:layout_height="110dp"android:layout_width="wrap_content"        android:layout_gravity="center_vertical"android:layout_marginLeft="5dp"android:layout_weight="1"/></LinearLayout><!-- Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_marginTop="18dp"android:textSize="18sp"android:text="    Next >   "/--></LinearLayout>

也可以到csdn下载:

http://download.csdn.net/detail/helloworldjavaee/5260020

更多相关文章

  1. android应用安全——组件通信安全(Intent)
  2. Android桌面小组件:最简例子
  3. Intent和Intent Filter的区别
  4. 构建自定义组件
  5. Android基础之Intent和Intent Filter
  6. Android学习(一)(初学)SharedPreferences数据库的使用
  7. android 之 Broadcast(广播) BroadcastReceiver(广播接收者)
  8. Intent 和 Intent Filter
  9. Android开发常用调试技术记录

随机推荐

  1. AndroidStudio3.5.1下搭建FFmpeg环境
  2. 系统去掉 Android(安卓)4.4.2 的StatusBa
  3. Android(安卓)Studio使用Gradle构建和发
  4. 解析Android中使用自定义字体的实现方法
  5. Android学习笔记---第五天---基础UI组件-
  6. android中textview常见属性设置
  7. Android(安卓)Wifi的工作流程
  8. Google Analytics Advanced Configuratio
  9. android中如何取得用户手机的常用联系人(
  10. Android(安卓)Native Crash的log分析和定