Code Monkey home page Code Monkey logo

country-region-picker-android's Introduction

country-region-picker-android

English

在做app登录的时候,因为需要支持国外手机号注册和登录,所以就涉及到国际电话区号的选择。在github上面找了一下,国家名称基本都是只有英文版本,而手动的去把中文一个个加上实在是一件费时费力的事情,所以就写了一段简单的java代码,抓取了某快递网站的数据转换成json格式,assets/code.json是处理后的数据。

支持以下语言:

  1. 简体中文
  2. 繁体中文
  3. 英文
  4. n种其他语言

依赖:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

...

implementation 'com.github.sahooz:country-region-picker-android:3.1.0'

提供两个版本的选择器:

  1. DialogFragment版本
    使用示例:
PickFragment.newInstance(new PickCallback() {
    @Override
    public void onPick(CountryOrRegion countryOrRegion) {
        if(countryOrRegion.flag != 0) ivFlag.setImageResource(countryOrRegion.flag);
        tvName.setText(countryOrRegion.name);
        tvCode.setText("+" + countryOrRegion.code);
    }
}).show(getSupportFragmentManager(), "countryOrRegion");

效果图:
img

  1. Activity版本
    使用示例
startActivityForResult(new Intent(getApplicationContext(), PickActivity.class), 111);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 111 && resultCode == Activity.RESULT_OK) {
        CountryOrRegion countryOrRegion = CountryOrRegion.fromJson(data.getStringExtra("countryOrRegion"));
        if(countryOrRegion.flag != 0) ivFlag.setImageResource(countryOrRegion.flag);
        tvName.setText(countryOrRegion.name);
        tvCode.setText("+" + countryOrRegion.code);
    }
}

效果图
img

另外,使用前和语言发生变化的时候,请初始化:

// 轻微耗时操作
CountryOrRegion.load(this);

销毁:

CountryOrRegion.destroy();

其实代码非常的简单,但是要把各个国家或者地区对应的中英文名称和国旗一一对应起来实在是一件费时费力的事情。供各位参考。

版本更新

3.1.0

  1. 国家/地区名相关bug修复
  2. 重命名一些包和类,使其更加合理,因为这些并不都是“国家”。所以升级的时候需要注意此部分修改

3.0

  1. 更换数据源,修正旧数据存在错误的问题
  2. 增加更多语言支持

country-region-picker-android's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.