地址选择器(三级)

可以自定义布局 先贴一个效果图

恩 我好像之前写过一份二级的地址选择器 可以去这里找到一份city.json 也就是地址文件 也可以找到依赖

先上一个解析的工具类


import android.content.Context;
import android.content.res.AssetManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class GetJsonDataUtil {
    public String getJson(Context context, String fileName) {

        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

}

下边贴代码 注释都有解释 我就不再重复了

private void initJsonData() {//解析数据
        /**
         * 注意:assets 目录下的Json文件仅供参考,实际使用可自行替换文件
         * 关键逻辑在于循环体
         * */
        String CityData = new GetJsonDataUtil().getJson(this, "city.json");//获取assets目录下的json文件数据

        options1Items = parseData(CityData);//用Gson 转成实体
        for (int i = 0; i < options1Items.size(); i++) {
            String areaName = options1Items.get(i).getAreaName();
            Log.i("地址", "省份" + areaName);
        }
        /**
         * 添加省份数据
         * 注意:如果是添加的JavaBean实体,则实体类需要实现 IPickerViewData 接口,
         * PickerView会通过getPickerViewText方法获取字符串显示出来。
         */

        //城市/市/区
        for (int i = 0; i < options1Items.size(); i++) {//遍历一个城市的市
            ArrayList<String> CityList = new ArrayList<>();//声明存放市的集合
            ArrayList<ArrayList<String>> CityList3 = new ArrayList<>();//声明存放区的集合
            for (int i1 = 0; i1 < options1Items.get(i).getCities().size(); i1++) {//遍历该省份的所有城市

                String name = options1Items.get(i).getCities().get(i1).getAreaName(); //获取二级城市名
                CityList.add(name);//添加城市

                ArrayList<String> CityList2 = new ArrayList<>();//声明存放区县的小集合
                for (int i2 = 0; i2 < options1Items.get(i).getCities().get(i1).getCounties().size(); i2++) {//遍历该市的全部区

                    String name3 = options1Items.get(i).getCities().get(i1).getCounties().get(i2).getAreaName(); //获取区
                    CityList2.add(name3);//添加城市

                }
                CityList3.add(CityList2);
            }
            /**
             * 添加城市数据
             * 把二级放入一级
             */
            options2Items.add(CityList);
            options3Items.add(CityList3);
        }

    }

    public ArrayList<textBean> parseData(String result) {//Gson 解析
        ArrayList<textBean> detail = new ArrayList<>();
        try {
            JSONArray data = new JSONArray(result);
            Gson gson = new Gson();
            for (int i = 0; i < data.length(); i++) {
                textBean entity = gson.fromJson(data.optJSONObject(i).toString(), textBean.class);
                detail.add(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return detail;
    }

    private void ziDingyi() {
        /**
         * @description
         *
         * 注意事项:
         * 自定义布局中,id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有,否则会报空指针。
         * 具体可参考demo 里面的两个自定义layout布局。
         */
        pvCustomOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                province = options1Items.get(options1).getAreaName();//一级城市 选中的值
                city = options2Items.get(options1).get(options2);
                three = options3Items.get(options1).get(options2).get(options3);
                if (province.equals(city)) {
                    mPersonalSite.setText(province + " " + three);//避免出现北京市北京市这种情况
                } else {
                    mPersonalSite.setText(province + " " + city + " " + three);//选定后显示的值
                }

            }
        })
                .setLayoutRes(R.layout.pickerview_custom_options, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        //自定义布局中的控件初始化及事件处理
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);//保存

                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomOptions.returnData();
                                pvCustomOptions.dismiss();
                            }
                        });

                    }
                })
                .setLineSpacingMultiplier(2.5f)//设置Item 的间距倍数,用于控制 Item 高度间隔
                .setContentTextSize(14)//滚轮文字大小
                .setDividerColor(Color.WHITE)//设置分割线的颜色 这几个值没需求的可以不要
                .build();

        pvCustomOptions.setPicker(options1Items, options2Items, options3Items);//三级选择器
        pvCustomOptions.show();
    }

这是储存数据的三个集合

 //地址选择器
    private ArrayList<textBean> options1Items = new ArrayList<>();//一级
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();//二级
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();//三级

这是自定义布局

R.layout.pickerview_custom_options
<?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="@drawable/bg_banyuan"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/white">

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="#aaa"
            android:visibility="gone" />

        <ImageView
            android:id="@+id/iv_cancel"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="16dp"
            android:padding="8dp"
            android:src="@drawable/touxiang"
            android:visibility="gone" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="城市"
            android:textColor="@color/text_color_2"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tv_finish"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="16dp"
            android:text="保存"
            android:textColor="#24AD9D"
            android:textSize="14sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="#aaa" />
    </RelativeLayout>

    <!--此部分需要完整复制过去,删减或者更改ID会导致初始化找不到内容而报空-->
    <LinearLayout
        android:id="@+id/optionspicker"
        android:layout_width="match_parent"
        android:layout_height="238dp"
        android:background="@android:color/white"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <com.contrarywind.view.WheelView
            android:id="@+id/options1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <com.contrarywind.view.WheelView
            android:id="@+id/options2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <com.contrarywind.view.WheelView
            android:id="@+id/options3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#24ad9d"
        android:visibility="gone" />

    <TextView
        android:id="@+id/tv_add"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#E5E9EC"
        android:gravity="center"
        android:text="+ 添加加油卡"
        android:textColor="#24AD9D"
        android:textSize="18sp"
        android:visibility="gone" />
</LinearLayout>

恩 貌似没有了 加油吧少年

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue级联选择器可以实现单项、二级和三级级联,甚至是多级级联。在web开发中,我们经常需要使用级联选择器来解决表单中的选择问题。根据引用,可以实现以下几种情况的级联选择器: 1. 单个级联:可以是下拉选择框或单选的形式。 2. 单个级联:可以是多项选择的形式。 3. 二级联动:例如省份和城市的级联选择。 4. 三级联动:例如省份、城市和区县的级联选择。 5. 多级级联:可以根据实际需求设置多级联动。 关于Vue级联选择器的具体实现和使用方法,可以参考引用中关于element组件的级联选择器的说明。通过组件嵌套的方式,可以实现级联选择器的功能。同时,引用中提到了实现数据的绑定以组件的形式调用级联选择器的功能。 因此,如果你想实现Vue级联选择器三级联动,可以按照上述引用内容的指导,使用Vue的级联选择器组件进行实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [基于vue2.0实现的级联选择器](https://download.csdn.net/download/weixin_38655987/14819357)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [VUE教你实现element级联选择器 | 多级菜单](https://blog.csdn.net/qq_34119437/article/details/89474796)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值