欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现拼多多地址选择器

程序员文章站 2022-07-04 10:33:05
本文实例为大家分享了android实现拼多多地址选择器的具体代码,供大家参考,具体内容如下突然想做一款地区选择器,然后我的弹框用的第三方的,地区数据用的是本地的json文件,解析json文件,然后把数...

本文实例为大家分享了android实现拼多多地址选择器的具体代码,供大家参考,具体内容如下

突然想做一款地区选择器,然后我的弹框用的第三方的,地区数据用的是本地的json文件,解析json文件,然后把数据放在list集合里面,然后设置到弹框里面,问题解决

源码下载地址

1.效果图

Android实现拼多多地址选择器

2.引用builder

implementation 'com.google.code.gson:gson:2.8.0'
implementation 'com.contrarywind:android-pickerview:4.1.9'

3.json文件存放地址:

e:\workspace\android\myapplication25\app\src\main\assets\citydata.json

4.解析类bean

package com.example.myapplication25;
 
 
import java.io.serializable;
import java.util.list;
 
public class citybean {
    /**
     * code : 200
     * message : 操作成功
     * datas : {"list":[{"area_id":"1","area_name":"北京","citylist":[{"area_id":"36","area_name":"北京市","arealist":[{"area_id":"37","area_name":"东城区"},{"area_id":"38","area_name":"西城区"},{"area_id":"41","area_name":"朝阳区"},{"area_id":"42","area_name":"丰台区"},{"area_id":"43","area_name":"石景山区"},{"area_id":"44","area_name":"海淀区"},{"area_id":"45","area_name":"门头沟区"},{"area_id":"46","area_name":"房山区"},{"area_id":"47","area_name":"通州区"},{"area_id":"48","area_name":"顺义区"},{"area_id":"49","area_name":"昌平区"},{"area_id":"50","area_name":"大兴区"},{"area_id":"51","area_name":"怀柔区"},{"area_id":"52","area_name":"平谷区"},{"area_id":"53","area_name":"密云县"},{"area_id":"54","area_name":"延庆县"},{"area_id":"566","area_name":"其他"}]}]}]}
     */
 
    private int code;
    private string message;
    private datasbean datas;
 
    public int getcode() {
        return code;
    }
 
    public void setcode(int code) {
        this.code = code;
    }
 
    public string getmessage() {
        return message;
    }
 
    public void setmessage(string message) {
        this.message = message;
    }
 
    public datasbean getdatas() {
        return datas;
    }
 
    public void setdatas(datasbean datas) {
        this.datas = datas;
    }
 
    public static class datasbean implements serializable {
        private list<listbean> list;
 
        public list<listbean> getlist() {
            return list;
        }
 
        public void setlist(list<listbean> list) {
            this.list = list;
        }
 
        public static class listbean implements serializable {
            /**
             * area_id : 1
             * area_name : 北京
             * citylist : [{"area_id":"36","area_name":"北京市","arealist":[{"area_id":"37","area_name":"东城区"},{"area_id":"38","area_name":"西城区"},{"area_id":"41","area_name":"朝阳区"},{"area_id":"42","area_name":"丰台区"},{"area_id":"43","area_name":"石景山区"},{"area_id":"44","area_name":"海淀区"},{"area_id":"45","area_name":"门头沟区"},{"area_id":"46","area_name":"房山区"},{"area_id":"47","area_name":"通州区"},{"area_id":"48","area_name":"顺义区"},{"area_id":"49","area_name":"昌平区"},{"area_id":"50","area_name":"大兴区"},{"area_id":"51","area_name":"怀柔区"},{"area_id":"52","area_name":"平谷区"},{"area_id":"53","area_name":"密云县"},{"area_id":"54","area_name":"延庆县"},{"area_id":"566","area_name":"其他"}]}]
             */
 
            private string area_id;
            private string area_name;
            private list<citylistbean> citylist;
 
            public string getarea_id() {
                return area_id;
            }
 
            public void setarea_id(string area_id) {
                this.area_id = area_id;
            }
 
            public string getarea_name() {
                return area_name;
            }
 
            public void setarea_name(string area_name) {
                this.area_name = area_name;
            }
 
            public list<citylistbean> getcitylist() {
                return citylist;
            }
 
            public void setcitylist(list<citylistbean> citylist) {
                this.citylist = citylist;
            }
 
            public static class citylistbean implements serializable {
                /**
                 * area_id : 36
                 * area_name : 北京市
                 * arealist : [{"area_id":"37","area_name":"东城区"},{"area_id":"38","area_name":"西城区"},{"area_id":"41","area_name":"朝阳区"},{"area_id":"42","area_name":"丰台区"},{"area_id":"43","area_name":"石景山区"},{"area_id":"44","area_name":"海淀区"},{"area_id":"45","area_name":"门头沟区"},{"area_id":"46","area_name":"房山区"},{"area_id":"47","area_name":"通州区"},{"area_id":"48","area_name":"顺义区"},{"area_id":"49","area_name":"昌平区"},{"area_id":"50","area_name":"大兴区"},{"area_id":"51","area_name":"怀柔区"},{"area_id":"52","area_name":"平谷区"},{"area_id":"53","area_name":"密云县"},{"area_id":"54","area_name":"延庆县"},{"area_id":"566","area_name":"其他"}]
                 */
 
                private string area_id;
                private string area_name;
                private list<arealistbean> arealist;
 
                public string getarea_id() {
                    return area_id;
                }
 
                public void setarea_id(string area_id) {
                    this.area_id = area_id;
                }
 
                public string getarea_name() {
                    return area_name;
                }
 
                public void setarea_name(string area_name) {
                    this.area_name = area_name;
                }
 
                public list<arealistbean> getarealist() {
                    return arealist;
                }
 
                public void setarealist(list<arealistbean> arealist) {
                    this.arealist = arealist;
                }
 
                public static class arealistbean implements serializable {
                    /**
                     * area_id : 37
                     * area_name : 东城区
                     */
 
                    private string area_id;
                    private string area_name;
 
                    public string getarea_id() {
                        return area_id;
                    }
 
                    public void setarea_id(string area_id) {
                        this.area_id = area_id;
                    }
 
                    public string getarea_name() {
                        return area_name;
                    }
 
                    public void setarea_name(string area_name) {
                        this.area_name = area_name;
                    }
                }
            }
        }
    }
}

5.弹框类

package com.example.myapplication25;
 
 
import android.app.activity;
import android.graphics.color;
 
import com.bigkoo.pickerview.builder.optionspickerbuilder;
import com.bigkoo.pickerview.listener.onoptionsselectchangelistener;
import com.bigkoo.pickerview.listener.onoptionsselectlistener;
import com.bigkoo.pickerview.view.optionspickerview;
 
public class selectpickerutil {
    protected static optionspickerview pvoptions;
 
 
    public static optionspickerbuilder getselectpickerbuilder2(optionspickerbuilder optionspickerbuilder) {
        optionspickerbuilder
                .settitletext("title")
                .setsubmitcolor(color.parsecolor("#11dbff"))
                .setcancelcolor(color.parsecolor("#999999"))
                .setdividercolor(color.gray)
                .setlinespacingmultiplier(2.2f)
                .settextcolorcenter(color.parsecolor("#11dbff")) //设置选中项文字颜色
                .setcontenttextsize(16);
        return optionspickerbuilder;
    }
 
    public static optionspickerbuilder getselectpickerbuilder3(optionspickerbuilder optionspickerbuilder) {
        optionspickerbuilder
                .setdividercolor(color.dkgray)
                .setcontenttextsize(20)
                .setsubmitcolor(color.parsecolor("#11dbff"))
                .setcancelcolor(color.parsecolor("#999999"))
                //                .setdecorview(mframelayout)//非dialog模式下,设置viewgroup, pickerview将会添加到这个viewgroup中
                .setoutsidecolor(0x00000000)
                .setlinespacingmultiplier(2.2f)
                .setoutsidecancelable(true);
        return optionspickerbuilder;
    }
 
    public static optionspickerview initchoicearea(activity activity, onoptionsselectlistener onoptionsselectlistener, onoptionsselectchangelistener onoptionsselectchangelistener) {
        pvoptions = new optionspickerbuilder(activity, onoptionsselectlistener)
                .setoptionsselectchangelistener(onoptionsselectchangelistener)
                .setsubmittext("确定")//确定按钮文字
                .setcanceltext("取消")//取消按钮文字
                .settitletext("城市选择")//标题
                .setsubcalsize(18)//确定和取消文字大小
                .settitlesize(20)//标题文字大小
                .settitlecolor(0xfff9731e)//标题文字颜色
                .setsubmitcolor(0xfff9731e)//确定按钮文字颜色
                .setcancelcolor(0xfff9731e)//取消按钮文字颜色
                .iscenterlabel(false) //是否只显示中间选中项的label文字,false则每项item全部都带有label。
                .setcyclic(false, false, false)//循环与否
                .setselectoptions(0, 0, 0)  //设置默认选中项
                .setoutsidecancelable(false)//点击外部dismiss default true
//                .isdialog(true)//是否显示为对话框样式
                .isrestoreitem(true)//切换时是否还原,设置默认选中第一项。
                .build();
        return pvoptions;
    }
 
}

6.主界面

package com.example.myapplication25;
 
 
import android.os.bundle;
import android.util.log;
import android.view.view;
import android.widget.button;
import android.widget.textview;
import com.bigkoo.pickerview.listener.onoptionsselectchangelistener;
import com.bigkoo.pickerview.listener.onoptionsselectlistener;
import com.bigkoo.pickerview.view.optionspickerview;
import com.google.gson.gson;
import java.io.ioexception;
import java.io.inputstream;
import java.util.arraylist;
import java.util.list;
 
import androidx.appcompat.app.appcompatactivity;
 
public class mainactivity extends appcompatactivity implements onoptionsselectlistener, onoptionsselectchangelistener {
    //参考网址:https://github.com/bigkoo/android-pickerview
    //  省
    protected list<string> options1items = new arraylist<>();
    protected list<string> city;
    //  市
    protected list<list<string>> options2items = new arraylist<>();
    //  区
    protected list<list<list<string>>> options3items = new arraylist<>();
    //  省地理
    protected list<string> options1itemsnumber = new arraylist<>();
    protected list<string> citynumber;
    protected list<list<string>> area;
    protected list<list<string>> areanumber;
    protected list<string> chirendenarea;
    protected list<string> chirendenareanumber;
    //  市地理
    protected list<list<string>> options2itemsnumber = new arraylist<>();
    //  区地理
    protected list<list<list<string>>> options3itemsnumber = new arraylist<>();
 
    private button btn_location;
    private textview tv_location;
 
    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);
 
        getcaseinfo2();
        btn_location = findviewbyid(r.id.btn_location);
        tv_location = findviewbyid(r.id.tv_location);
        btn_location.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view view) {
                anxzxz();
            }
        });
    }
 
    public void anxzxz() {
        optionspickerview build = selectpickerutil.initchoicearea(mainactivity.this, this, this);
        build.setpicker(options1items, options2items, options3items);//添加数据源
        build.show();
    }
 
    private void getcaseinfo2() {
        string json = null;
        try {
            inputstream inputstream = getassets().open("citydata.json");
            int size = inputstream.available();
            byte[] buffer = new byte[size];
            inputstream.read(buffer);
            inputstream.close();
            json = new string(buffer, "utf-8");
        } catch (ioexception ex) {
            ex.printstacktrace();
        }
        //  log.e("tag","loadjsonfromassests_all:"+json.tostring());
        gson gson = new gson();
        citybean usersimple = gson.fromjson(json, citybean.class);
        for (int i = 0; i < usersimple.getdatas().getlist().size(); i++) {
            options1items.add(usersimple.getdatas().getlist().get(i).getarea_name());
            options1itemsnumber.add(usersimple.getdatas().getlist().get(i).getarea_id());
            city = new arraylist<>();
            citynumber = new arraylist<>();
            area = new arraylist<>();
            areanumber = new arraylist<>();
            list<citybean.datasbean.listbean.citylistbean> citylist = usersimple.getdatas().getlist().get(i).getcitylist();
            for (int o = 0; o < usersimple.getdatas().getlist().get(i).getcitylist().size(); o++) {
                city.add(citylist.get(o).getarea_name());
                citynumber.add(citylist.get(o).getarea_id());
                chirendenarea = new arraylist<>();
                chirendenareanumber = new arraylist<>();
                list<citybean.datasbean.listbean.citylistbean.arealistbean> arealist = usersimple.getdatas().getlist().get(i).getcitylist().get(o).getarealist();
                for (int u = 0; u < usersimple.getdatas().getlist().get(i).getcitylist().get(o).getarealist().size(); u++) {
                    chirendenarea.add(arealist.get(u).getarea_name());
                    chirendenareanumber.add(arealist.get(u).getarea_id());
                }
                area.add(chirendenarea);
                areanumber.add(chirendenareanumber);
            }
            options2items.add(city);
            options2itemsnumber.add(citynumber);
            options3items.add(area);
            options3itemsnumber.add(areanumber);
        }
    }
 
    @override
    public void onoptionsselectchanged(int options1, int options2, int options3) {
 
    }
 
    @override
    public void onoptionsselect(int options1, int options2, int options3, view view) {
        tv_location.settext(string.format("%s省--%s--%s",
                options1items.get(options1),
                options2items.get(options1).get(options2),
                options3items.get(options1).get(options2).get(options3)));
        log.e("tag", "onoptionsselect_end:" +
                options1itemsnumber.get(options1) + options1items.get(options1)
                + options2itemsnumber.get(options1).get(options2) + options2items.get(options1).get(options2)
                + options3itemsnumber.get(options1).get(options2).get(options3) + options3items.get(options1).get(options2).get(options3));
    }
}

7.布局页面就是一个button和一个textview,请自行添加

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。