Android-省市县地址选择
使用开源组件,基于公开的省市县数据源+本地化,以 http 接口形式供 App 调用。本地化是因为有些区县数据还未纳入正式版中,但又要满足客户需求
步骤
app/build.gradle
1
2
3implementation 'com.squareup.okhttp3:okhttp:3.3.1'
implementation 'com.github.gzu-liyujiang.AndroidPicker:WheelPicker:4.0.1'
implementation 'com.github.gzu-liyujiang.AndroidPicker:AddressPicker:4.0.1'CityUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84package com.example.demo;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import com.github.gzuliyujiang.wheelpicker.AddressPicker;
import com.github.gzuliyujiang.wheelpicker.contract.AddressParser;
import com.github.gzuliyujiang.wheelpicker.contract.OnLinkageSelectedListener;
import com.github.gzuliyujiang.wheelpicker.entity.ProvinceEntity;
import com.github.gzuliyujiang.wheelpicker.utility.AddressJsonParser;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import okhttp3.OkHttpClient;
import okhttp3.Request;
public class CityUtil {
public static final String SERVER_URL = "http://192.168.0.197:8080/gms-web/citys/allData.do";
private static List<ProvinceEntity> provinceEntityList = null;
public static void init(AddressPicker picker) {
if(provinceEntityList == null) {
Executors.newSingleThreadExecutor().execute(new Runnable() {
public void run() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(SERVER_URL).build();
okhttp3.Response response = null;
try {
response = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
String text = null;
try {
text = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
if (TextUtils.isEmpty(text)) {
provinceEntityList = new ArrayList<>();
} else {
AddressParser parser = new AddressJsonParser.Builder()
.provinceCodeField("code")
.provinceNameField("name")
.provinceChildField("children")
.cityCodeField("code")
.cityNameField("name")
.cityChildField("children")
.countyCodeField("code")
.countyNameField("name")
.build();
provinceEntityList = parser.parseData(text);
}
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
picker.onAddressReceived(provinceEntityList);
updateTitle(picker);
}
});
}
});
} else {
picker.onAddressReceived(provinceEntityList);
updateTitle(picker);
}
}
private static void updateTitle(AddressPicker picker) {
picker.getWheelLayout().setOnLinkageSelectedListener(new OnLinkageSelectedListener() {
public void onLinkageSelected(Object first, Object second, Object third) {
picker.getTitleView().setText(String.format("%s%s%s",
picker.getFirstWheelView().formatItem(first),
picker.getSecondWheelView().formatItem(second),
picker.getThirdWheelView().formatItem(third)));
}
});
}
}MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71package com.example.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.github.gzuliyujiang.wheelpicker.AddressPicker;
import com.github.gzuliyujiang.wheelpicker.contract.AddressParser;
import com.github.gzuliyujiang.wheelpicker.contract.OnAddressPickedListener;
import com.github.gzuliyujiang.wheelpicker.contract.OnLinkageSelectedListener;
import com.github.gzuliyujiang.wheelpicker.entity.CityEntity;
import com.github.gzuliyujiang.wheelpicker.entity.CountyEntity;
import com.github.gzuliyujiang.wheelpicker.entity.ProvinceEntity;
import com.github.gzuliyujiang.wheelpicker.utility.AddressJsonParser;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import okhttp3.OkHttpClient;
import okhttp3.Request;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView address_view = findViewById(R.id.address);
Button chooseAddress = findViewById(R.id.chooseAddress);
chooseAddress.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showAddressPicker(address_view);
}
});
}
private void showAddressPicker(TextView textView) {
AddressPicker picker = new AddressPicker(this);
CityUtil.init(picker);
//默认显示
picker.setDefaultValue("湖北省", "荆州市", "开发区");
picker.setOnAddressPickedListener(new OnAddressPickedListener() {
public void onAddressPicked(ProvinceEntity province, CityEntity city, CountyEntity county) {
//区县编码
String code = county.getCode();
//省市县名称
String name = province.getName() + city.getName() + county.getName();
textView.setText(code);
Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
}
});
picker.show();
}
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 王文哲的博客!