Android - 城市/单项/国家区号选择器基础使用 及 使用国际区号json文件

1. citypickerview的城市选择器:

Github:https://github.com/androidzhangjin/citypicker

  1. 添加依赖
implementation 'liji.library.dev:citypickerview:5.2.2'
  1. 初始化选择器
private CityPickerView cityPickerView;
@Override
    protected void onCreate(Bundle savedInstanceState) {
   
   		 cityPickerView = new CityPickerView();
        cityPickerView.init(this);
    }
  1. 设置参数
//设置默认地址:从服务器拿的设置过的地址
String[] addressSplit = receivingAddress.getAddress().split("-");
CityConfig cityConfig = new CityConfig.Builder()
		.cancelTextSize(14)
		.confirmTextSize(14)
		.title("地址选择")
		.titleBackgroundColor(IS_NIGHT_MODE ? "#1E212D" : "#FFFFFF")
		.titleTextColor(IS_NIGHT_MODE ? "#8597A5" : "#696969")
		.confirTextColor(IS_NIGHT_MODE ? "#8597A5" : "#696969")
		.cancelTextColor(IS_NIGHT_MODE ? "#8597A5" : "#696969")
		.provinceCyclic(true)
		.cityCyclic(false)
		.districtCyclic(false)
		.visibleItemsCount(7)
		.province(addressSplit[0])
		.city(addressSplit[1])
		.district(addressSplit[2])
		.build();
cityPickerView.setConfig(cityConfig);
cityPickerView.setOnCityItemClickListener(new OnCityItemClickListener() {
   
	@Override
	public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
   
		super.onSelected(province, city, district);
		tvAddress.setText(province + "-" + city + "-" + district);
	});  
  1. 点击显示选择器
 public void onClickView(View view) {
   
	 KeyboardUtil.hideSoftKeyboard(this);
	 if (cityPickerView != null) {
   
	 	cityPickerView.showCityPicker();
	 }
 }
2. Android-PickerView的单项选择器

GitHub:https://github.com/Bigkoo/Android-PickerView 也有时间选择器
导入依赖:

implementation 'com.contrarywind:Android-PickerView:3.2.7'
		tvCountry.setOnClickListener(view -> {
   
            if(!pvOptions.isShowing()){
   
                pvOptions.setPicker(bankNameList);
                pvOptions.show();
            }
        });

        pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
   
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
   
                String[] select = bankNameList.get(options1).split(" ");
                tvCountry.setText("+"+select[0]);
            }
        }).setSubmitText(getString(R.string.confirm))
                .setCancelText(getString(R.string.cancel))
                .setTitleText("")//标题
                .setSubCalSize(15)//确定和取消文字大小
                .setContentTextSize(15)//滚轮文字大小
                .setTitleSize(16)//标题文字大小
                .setTitleColor(getResources().getColor(R.color.text_black))//标题文字颜色
                .setSubmitColor(getResources().getColor(R.color.red_dc3c23))//确定按钮文字颜色
                .setCancelColor(getResources().getColor(R.color.text_black))//取消按钮文字颜色
                .setTextColorOut(getResources().getColor(R.color.text_black))
                .setTitleBgColor(getResources().getColor(R.color.white))//标题背景颜色 Night mode
                .setTextColorCenter(getResources().getColor(R.color.red_dc3c23))
                .setBgColor(getResources().getColor(R.color.white))//滚轮背景颜色 Night mode
                .isCenterLabel(false) //是否只显示中间选中项的label文字,false则每项item全部都带有label。
                .setSelectOptions(5)  //设置默认选中项
                .setOutSideCancelable(false)//点击外部dismiss default true
                .isDialog(false)//是否显示为对话框样式
                .build();
3. CountryCodePicker国家区号选择器

Github:https://github.com/joielechong/CountryCodePicker

implementation 'com.hbb20:ccp:2.6.1'
<com.hbb20.CountryCodePicker
    android:id="@+id/ccp"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    app:ccp_textGravity="LEFT"
    android:gravity="center_vertical"
    android:paddingLeft="5dp"
    app:ccpDialog_background="@color/white"
    app:ccpDialog_cornerRadius="15dp"
    app:ccpDialog_showCloseIcon="true"
    app:ccpDialog_showFlag="true"
    app:ccpDialog_showPhoneCode="true"
    app:ccp_arrowColor="@color/white"
    app:ccp_contentColor="@color/text_black"
    app:ccp_defaultNameCode="SG" 
    app:ccp_showFullName="true"
    app:ccp_showPhoneCode="false"
    app:ccp_textSize="15sp" />
//获取选择的区号
ccp.setOnCountryChangeListener(() ->{
   
  	tvArea.setText(ccp.getSelectedCountryCode());
});
//        ccp.registerCarrierNumberEditText(tvArea);
4. 使用国际区号json文件(与3无关)
  1. 添加依赖
 implementation 'com.google.code.gson:gson:2.8.6'
  1. 将area.json导入到assets下
{
   
  "data": [
    {
   
      "shortName": "AD",
      "name": "安道尔共和国",
      "en": "Andorra",
      "tel": "376",
      "pinyin": "adeghg"
    },....
    完整版在下面
  1. 实体类
public class AreasModel {
   
    private List<CountryModel> data;

    public void setData(List<CountryModel> data) {
   
        this.data = data;
    }

    public List<CountryModel> getData() {
   
        return this.data;
    }

    public class CountryModel {
   
        public String name; //名字
        public String shortName; //首字母
        public String tel; //编号
        public String en;
        public String pinyin;
}
  1. 读取json转换成列表
private void initView(){
   
        String json = getJsonData(this, "area.json");
        AreasModel areasModel = new Gson().fromJson(json, AreasModel.class);
        countryList = (ArrayList<AreasModel.CountryModel>) areasModel.getData();
}

public static String getJsonData(Context context, String fileName) {
   
        StringBuilder stringBuilder = new StringBuilder();
        try {
   
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    context.getResources().getAssets().open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
   
                stringBuilder.append(line);
            }
        } catch (IOException e) {
   
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

ok 结束,以下是area.json文件:

{
   
  "data": [
    {
   
      "shortName": "AD",
      "name": "安道尔共和国",
      "en": "Andorra",
      "tel": "376",
      "pinyin": "adeghg"
    },
    {
   
      "shortName": "AE",
      "name": "阿拉伯联合酋长国",
      "en": "UnitedArabEmirates",
      "tel": "971",
      "pinyin": "alblhqzg"
    },
    {
   
      "shortName": "AF",
      "name": "阿富汗",
      "en": "Afghanistan",
      "tel": "93",
      "pinyin": "afh"
    },
    {
   
      "shortName": "AG",
      "name": "安提瓜和巴布达",
      "en": "AntiguaandBarbuda",
      "tel": "1268",
      "pinyin": "atghbbd"
    },
    {
   
      "shortName": "AI",
      "name": "安圭拉岛",
      "en": "Anguilla",
      "tel": "1264",
      "pinyin": "agld"
    },
    {
   
      "shortName": "AL",
      "name": "阿尔巴尼亚",
      "en": "Albania",
      "tel": "355",
      "pinyin": "aebny"
    },
    {
   
      "shortName": "AM",
      "name": "阿美尼亚",
      "en": "Armenia",
      "tel": "374",
      "pinyin": "amny"
    },
    {
   
      "shortName": "",
      "name": "阿森松",
      "en": "Ascension",
      "tel": "247",
      "pinyin": "als"

    },
    {
   
      "shortName": "AO",
      "name": "安哥拉",
      "en": "Angola",
      "tel": "244",
      "pinyin": "agl"

    },
    {
   
      "shortName": "AR",
      "name": "阿根廷",
      "en": "Argentina",
      "tel": "54",
      "pinyin": "agt"

    },
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值