Android开发中姓氏与名字分离汉字翻译成拼音

近期在项目开发过程中遇到过一个给你一个中文名字,然后将姓与名字拆分翻译成中文的需求,样例见图一:

图1

需求中有两个关键点:如何进行姓氏与名字拆分,和如何将汉字翻译成拼音

由于开发经验较少,本着学习的态度,于是先在网上查找各位前辈有什么解决办法,转拼音倒是

挺简单的,网上有开源的pinyin4j拿着用就行了。难点在于姓氏与名字拆分,毕竟名字可能复姓,乃至于更稀奇的姓氏。

经查找资料倒是挺少的,最后决定采用下面这个作者的思路https://blog.csdn.net/qq136490497/article/details/79698014

但是由于这个作者只提供了思路,关键方法并没有提供,于是稍微自己进行完善,并利用他的姓氏表:

现将代码直接贴出,具体可见注释:

首先倒入依赖包:

implementation'com.github.open-android:pinyin4j:2.5.0'

//Gson

implementation'com.google.code.gson:gson:2.8.5'

创建实体类:NameInfo

class NameInfo {

private Stringsurname;

public String getSurname() {

return surname;

}

public void setSurname(String surname) {

this.surname = surname;

}

}

然后进入主体代码编写:

public class MainActivityextends AppCompatActivityimplements View.OnClickListener {

private Buttonbutton1;

private Buttonbutton2;

private Stringtext;

private EditTextcontent;

private

static Stringjson

="{\"RECORDS\":[{\"surname\":\"李\"},{\"surname\":\"纳兰\"},{\"surname\":\"王\"},{\"surname\":\"张\"},{\"surname\":\"刘\"},{\"surname\":\"陈\"},{\"surname\":\"杨\"},{\"surname\":\"赵\"},{\"surname\":\"黄\"},{\"surname\":\"周\"},{\"surname\":\"吴\"},{\"surname\":\"徐\"},{\"surname\":\"孙\"},{\"surname\":\"胡\"},{\"surname\":\"朱\"},{\"surname\":\"高\"},{\"surname\":\"林\"},{\"surname\":\"何\"},{\"surname\":\"郭\"},{\"surname\":\"夏侯\"},{\"surname\":\"马\"},{\"surname\":\"罗\"},{\"surname\":\"梁\"},{\"surname\":\"宋\"},{\"surname\":\"郑\"},{\"surname\":\"谢\"},{\"surname\":\"韩\"},{\"surname\":\"唐\"},{\"surname\":\"冯\"},{\"surname\":\"于\"},{\"surname\":\"董\"},{\"surname\":\"萧\"},{\"surname\":\"程\"},{\"surname\":\"曹\"},{\"surname\":\"袁\"},{\"surname\":\"邓\"},{\"surname\":\"许\"},{\"surname\":\"傅\"},{\"surname\":\"沈\"},{\"surname\":\"曾\"},{\"surname\":\"彭\"},{\"surname\":\"吕\"},{\"surname\":\"苏\"},{\"surname\":\"卢\"},{\"surname\":\"蒋\"},{\"surname\":\"蔡\"},{\"surname\":\"魏\"},{\"surname\":\"贾\"},{\"surname\":\"丁\"},{\"surname\":\"薛\"},{\"surname\":\"叶\"},{\"surname\":\"阎\"},{\"surname\":\"余\"},{\"surname\":\"潘\"},{\"surname\":\"杜\"},{\"surname\":\"戴\"},{\"surname\":\"夏\"},{\"surname\":\"钟\"},{\"surname\":\"汪\"},{\"surname\":\"田\"},{\"surname\":\"任\"},{\"surname\":\"姜\"},{\"surname\":\"范\"},{\"surname\":\"方\"},{\"surname\":\"石\"},{\"surname\":\"姚\"},{\"surname\":\"谭\"},{\"surname\":\"廖\"},{\"surname\":\"邹\"},{\"surname\":\"熊\"},{\"surname\":\"金\"},{\"surname\":\"陆\"},{\"surname\":\"郝\"},{\"surname\":\"孔\"},{\"surname\":\"白\"},{\"surname\":\"崔\"},{\"surname\":\"康\"},{\"surname\":\"毛\"},{\"surname\":\"邱\"},{\"surname\":\"秦\"},{\"surname\":\"江\"},{\"surname\":\"史\"},{\"surname\":\"顾\"},{\"surname\":\"侯\"},{\"surname\":\"邵\"},{\"surname\":\"孟\"},{\"surname\":\"龙\"},{\"surname\":\"万\"},{\"surname\":\"段\"},{\"surname\":\"雷\"},{\"surname\":\"钱\"},{\"surname\":\"汤\"},{\"surname\":\"尹\"},{\"surname\":\"易\"},{\"surname\":\"黎\"},{\"surname\":\"常\"},{\"surname\":\"武\"},{\"surname\":\"乔\"},{\"surname\":\"贺\"},{\"surname\":\"赖\"},{\"surname\":\"龚\"},{\"surname\":\"文\"},{\"surname\":\"欧阳\"},{\"surname\":\"爱新觉罗\"},{\"surname\":\"褚\"},{\"surname\":\"卫\"},{\"surname\":\"尤\"},{\"surname\":\"施\"},{\"surname\":\"严\"},{\"surname\":\"华\"},{\"surname\":\"戚\"},{\"surname\":\"喻\"},{\"surname\":\"柏\"},{\"surname\":\"水\"},{\"surname\":\"窦\"},{\"surname\":\"章\"},{\"surname\":\"云\"},{\"surname\":\"葛\"},{\"surname\":\"奚\"},{\"surname\":\"郎\"},{\"surname\":\"鲁\"},{\"surname\":\"韦\"},{\"surname\":\"昌\"},{\"surname\":\"苗\"},{\"surname\":\"凤\"},{\"surname\":\"花\"},{\"surname\":\"俞\"},{\"surname\":\"柳\"},{\"surname\":\"酆\"},{\"surname\":\"鲍\"},{\"surname\":\"费\"},{\"surname\":\"廉\"},{\"surname\":\"岑\"},{\"surname\":\"倪\"},{\"surname\":\"滕\"},{\"surname\":\"殷\"},{\"surname\":\"毕\"},{\"surname\":\"邬\"},{\"surname\":\"安\"},{\"surname\":\"乐\"},{\"surname\":\"时\"},{\"surname\":\"皮\"},{\"surname\":\"卞\"},{\"surname\":\"齐\"},{\"surname\":\"伍\"},{\"surname\":\"元\"},{\"surname\":\"卜\"},{\"surname\":\"平\"},{\"surname\":\"和\"},{\"surname\":\"穆\"},{\"surname\":\"湛\"},{\"surname\":\"祁\"},{\"surname\":\"禹\"},{\"surname\":\"狄\"},{\"surname\":\"米\"},{\"surname\":\"贝\"},{\"surname\":\"明\"},{\"surname\":\"臧\"},{\"surname\":\"计\"},{\"surname\":\"伏\"},{\"surname\":\"成\"},{\"surname\":\"谈\"},{\"surname\":\"茅\"},{\"surname\":\"庞\"},{\"surname\":\"纪\"},{\"surname\":\"舒\"},{\"surname\":\"屈\"},{\"surname\":\"项\"},{\"surname\":\"祝\"},{\"surname\":\"阮\"},{\"surname\":\"蓝\"},{\"surname\":\"闵\"},{\"surname\":\"席\"},{\"surname\":\"季\"},{\"surname\":\"麻\"},{\"surname\":\"强\"},{\"surname\":\"路\"},{\"surname\":\"娄\"},{\"surname\":\"危\"},{\"surname\":\"童\"},{\"surname\":\"颜\"},{\"surname\":\"梅\"},{\"surname\":\"盛\"},{\"surname\":\"刁\"},{\"surname\":\"锺\"},{\"surname\":\"宣\"},{\"surname\":\"贲\"},{\"surname\":\"郁\"},{\"surname\":\"单\"},{\"surname\":\"杭\"},{\"surname\":\"洪\"},{\"surname\":\"包\"},{\"surname\":\"诸\"},{\"surname\":\"左\"},{\"surname\":\"吉\"},{\"surname\":\"钮\"},{\"surname\":\"嵇\"},{\"surname\":\"邢\"},{\"surname\":\"滑\"},{\"surname\":\"裴\"},{\"surname\":\"荣\"},{\"surname\":\"翁\"},{\"surname\":\"荀\"},{\"surname\":\"羊\"},{\"surname\":\"於\"},{\"surname\":\"惠\"},{\"surname\":\"甄\"},{\"surname\":\"麴\"},{\"surname\":\"家\"},{\"surname\":\"封\"},{\"surname\":\"芮\"},{\"surname\":\"羿\"},{\"surname\":\"储\"},{\"surname\":\"靳\"},{\"surname\":\"汲\"},{\"surname\":\"邴\"},{\"surname\":\"糜\"},{\"surname\":\"松\"},{\"surname\":\"井\"},{\"surname\":\"富\"},{\"surname\":\"巫\"},{\"surname\":\"乌\"},{\"surname\":\"焦\"},{\"surname\":\"巴\"},{\"surname\":\"弓\"},{\"surname\":\"牧\"},{\"surname\":\"隗\"},{\"surname\":\"山\"},{\"surname\":\"谷\"},{\"surname\":\"车\"},{\"surname\":\"宓\"},{\"surname\":\"蓬\"},{\"surname\":\"全\"},{\"surname\":\"郗\"},{\"surname\":\"班\"},{\"surname\":\"仰\"},{\"surname\":\"秋\"},{\"surname\":\"仲\"},{\"surname\":\"伊\"},{\"surname\":\"宫\"},{\"surname\":\"宁\"},{\"surname\":\"仇\"},{\"surname\":\"栾\"},{\"surname\":\"暴\"},{\"surname\":\"甘\"},{\"surname\":\"钭\"},{\"surname\":\"历\"},{\"surname\":\"戎\"},{\"surname\":\"祖\"},{\"surname\":\"符\"},{\"surname\":\"景\"},{\"surname\":\"詹\"},{\"surname\":\"束\"},{\"surname\":\"幸\"},{\"surname\":\"司\"},{\"surname\":\"韶\"},{\"surname\":\"郜\"},{\"surname\":\"蓟\"},{\"surname\":\"溥\"},{\"surname\":\"印\"},{\"surname\":\"宿\"},{\"surname\":\"怀\"},{\"surname\":\"蒲\"},{\"surname\":\"邰\"},{\"surname\":\"从\"},{\"surname\":\"鄂\"},{\"surname\":\"索\"},{\"surname\":\"咸\"},{\"surname\":\"籍\"},{\"surname\":\"卓\"},{\"surname\":\"蔺\"},{\"surname\":\"屠\"},{\"surname\":\"蒙\"},{\"surname\":\"池\"},{\"surname\":\"阳\"},{\"surname\":\"胥\"},{\"surname\":\"能\"},{\"surname\":\"苍\"},{\"surname\":\"双\"},{\"surname\":\"闻\"},{\"surname\":\"莘\"},{\"surname\":\"党\"},{\"surname\":\"贡\"},{\"surname\":\"劳\"},{\"surname\":\"逄\"},{\"surname\":\"姬\"},{\"surname\":\"申\"},{\"surname\":\"扶\"},{\"surname\":\"堵\"},{\"surname\":\"冉\"},{\"surname\":\"宰\"},{\"surname\":\"郦\"},{\"surname\":\"雍\"},{\"surname\":\"却\"},{\"surname\":\"璩\"},{\"surname\":\"桑\"},{\"surname\":\"桂\"},{\"surname\":\"濮\"},{\"surname\":\"牛\"},{\"surname\":\"寿\"},{\"surname\":\"通\"},{\"surname\":\"边\"},{\"surname\":\"扈\"},{\"surname\":\"燕\"},{\"surname\":\"冀\"},{\"surname\":\"僪\"},{\"surname\":\"浦\"},{\"surname\":\"尚\"},{\"surname\":\"农\"},{\"surname\":\"温\"},{\"surname\":\"别\"},{\"surname\":\"庄\"},{\"surname\":\"晏\"},{\"surname\":\"柴\"},{\"surname\":\"充\"},{\"surname\":\"慕\"},{\"surname\":\"连\"},{\"surname\":\"茹\"},{\"surname\":\"习\"},{\"surname\":\"宦\"},{\"surname\":\"艾\"},{\"surname\":\"鱼\"},{\"surname\":\"容\"},{\"surname\":\"向\"},{\"surname\":\"古\"},{\"surname\":\"慎\"},{\"surname\":\"戈\"},{\"surname\":\"庾\"},{\"surname\":\"终\"},{\"surname\":\"暨\"},{\"surname\":\"居\"},{\"surname\":\"衡\"},{\"surname\":\"步\"},{\"surname\":\"都\"},{\"surname\":\"耿\"},{\"surname\":\"满\"},{\"surname\":\"弘\"},{\"surname\":\"匡\"},{\"surname\":\"国\"},{\"surname\":\"寇\"},{\"surname\":\"广\"},{\"surname\":\"禄\"},{\"surname\":\"阙\"},{\"surname\":\"东\"},{\"surname\":\"欧\"},{\"surname\":\"殳\"},{\"surname\":\"沃\"},{\"surname\":\"利\"},{\"surname\":\"蔚\"},{\"surname\":\"越\"},{\"surname\":\"夔\"},{\"surname\":\"隆\"},{\"surname\":\"师\"},{\"surname\":\"巩\"},{\"surname\":\"厍\"},{\"surname\":\"聂\"},{\"surname\":\"晁\"},{\"surname\":\"勾\"},{\"surname\":\"敖\"},{\"surname\":\"融\"},{\"surname\":\"冷\"},{\"surname\":\"訾\"},{\"surname\":\"辛\"},{\"surname\":\"阚\"},{\"surname\":\"那\"},{\"surname\":\"简\"},{\"surname\":\"饶\"},{\"surname\":\"空\"},{\"surname\":\"毋\"},{\"surname\":\"沙\"},{\"surname\":\"乜\"},{\"surname\":\"桓\"},{\"surname\":\"公\"},{\"surname\":\"万俟\"},{\"surname\":\"司马\"},{\"surname\":\"上官\"},{\"surname\":\"诸葛\"},{\"surname\":\"闻人\"},{\"surname\":\"东方\"},{\"surname\":\"赫连\"},{\"surname\":\"皇甫\"},{\"surname\":\"尉迟\"},{\"surname\":\"公羊\"},{\"surname\":\"澹台\"},{\"surname\":\"公冶\"},{\"surname\":\"宗政\"},{\"surname\":\"濮阳\"},{\"surname\":\"淳于\"},{\"surname\":\"单于\"},{\"surname\":\"太叔\"},{\"surname\":\"申屠\"},{\"surname\":\"公孙\"},{\"surname\":\"仲孙\"},{\"surname\":\"轩辕\"},{\"surname\":\"令狐\"},{\"surname\":\"钟离\"},{\"surname\":\"宇文\"},{\"surname\":\"长孙\"},{\"surname\":\"慕容\"},{\"surname\":\"司徒\"},{\"surname\":\"司空\"},{\"surname\":\"召\"},{\"surname\":\"有\"},{\"surname\":\"舜\"},{\"surname\":\"叶赫那拉\"},{\"surname\":\"丛\"},{\"surname\":\"岳\"},{\"surname\":\"寸\"},{\"surname\":\"贰\"},{\"surname\":\"皇\"},{\"surname\":\"侨\"},{\"surname\":\"彤\"},{\"surname\":\"竭\"},{\"surname\":\"端\"},{\"surname\":\"赫\"},{\"surname\":\"实\"},{\"surname\":\"甫\"},{\"surname\":\"集\"},{\"surname\":\"象\"},{\"surname\":\"翠\"},{\"surname\":\"狂\"},{\"surname\":\"辟\"},{\"surname\":\"典\"},{\"surname\":\"良\"},{\"surname\":\"函\"},{\"surname\":\"芒\"},{\"surname\":\"苦\"},{\"surname\":\"其\"},{\"surname\":\"京\"},{\"surname\":\"中\"},{\"surname\":\"夕\"},{\"surname\":\"之\"},{\"surname\":\"章佳\"},{\"surname\":\"那拉\"},{\"surname\":\"冠\"},{\"surname\":\"宾\"},{\"surname\":\"香\"},{\"surname\":\"果\"},{\"surname\":\"依尔根觉罗\"},{\"surname\":\"依尔觉罗\"},{\"surname\":\"萨嘛喇\"},{\"surname\":\"赫舍里\"},{\"surname\":\"额尔德特\"},{\"surname\":\"萨克达\"},{\"surname\":\"钮祜禄\"},{\"surname\":\"他塔喇\"},{\"surname\":\"喜塔腊\"},{\"surname\":\"讷殷富察\"},{\"surname\":\"叶赫那兰\"},{\"surname\":\"库雅喇\"},{\"surname\":\"瓜尔佳\"},{\"surname\":\"舒穆禄\"},{\"surname\":\"索绰络\"},{\"surname\":\"纳喇\"},{\"surname\":\"乌雅\"},{\"surname\":\"范姜\"},{\"surname\":\"碧鲁\"},{\"surname\":\"张廖\"},{\"surname\":\"张简\"},{\"surname\":\"图门\"},{\"surname\":\"太史\"},{\"surname\":\"公叔\"},{\"surname\":\"乌孙\"},{\"surname\":\"完颜\"},{\"surname\":\"马佳\"},{\"surname\":\"佟佳\"},{\"surname\":\"富察\"},{\"surname\":\"费莫\"},{\"surname\":\"蹇\"},{\"surname\":\"称\"},{\"surname\":\"诺\"},{\"surname\":\"来\"},{\"surname\":\"多\"},{\"surname\":\"繁\"},{\"surname\":\"戊\"},{\"surname\":\"朴\"},{\"surname\":\"回\"},{\"surname\":\"毓\"},{\"surname\":\"税\"},{\"surname\":\"荤\"},{\"surname\":\"靖\"},{\"surname\":\"绪\"},{\"surname\":\"愈\"},{\"surname\":\"硕\"},{\"surname\":\"牢\"},{\"surname\":\"买\"},{\"surname\":\"但\"},{\"surname\":\"巧\"},{\"surname\":\"枚\"},{\"surname\":\"撒\"},{\"surname\":\"泰\"},{\"surname\":\"秘\"},{\"surname\":\"亥\"},{\"surname\":\"绍\"},{\"surname\":\"以\"},{\"surname\":\"壬\"},{\"surname\":\"森\"},{\"surname\":\"斋\"},{\"surname\":\"释\"},{\"surname\":\"奕\"},{\"surname\":\"姒\"},{\"surname\":\"朋\"},{\"surname\":\"求\"},{\"surname\":\"羽\"},{\"surname\":\"用\"},{\"surname\":\"占\"},{\"surname\":\"真\"},{\"surname\":\"穰\"},{\"surname\":\"翦\"},{\"surname\":\"闾\"},{\"surname\":\"漆\"},{\"surname\":\"贵\"},{\"surname\":\"代\"},{\"surname\":\"贯\"},{\"surname\":\"旁\"},{\"surname\":\"崇\"},{\"surname\":\"栋\"},{\"surname\":\"告\"},{\"surname\":\"休\"},{\"surname\":\"褒\"},{\"surname\":\"谏\"},{\"surname\":\"锐\"},{\"surname\":\"皋\"},{\"surname\":\"闳\"},{\"surname\":\"在\"},{\"surname\":\"歧\"},{\"surname\":\"禾\"},{\"surname\":\"示\"},{\"surname\":\"是\"},{\"surname\":\"委\"},{\"surname\":\"钊\"},{\"surname\":\"频\"},{\"surname\":\"嬴\"},{\"surname\":\"呼\"},{\"surname\":\"大\"},{\"surname\":\"威\"},{\"surname\":\"昂\"},{\"surname\":\"律\"},{\"surname\":\"冒\"},{\"surname\":\"保\"},{\"surname\":\"系\"},{\"surname\":\"不\"},{\"surname\":\"户\"},{\"surname\":\"闭\"},{\"surname\":\"才\"},{\"surname\":\"无\"},{\"surname\":\"书\"},{\"surname\":\"学\"},{\"surname\":\"愚\"},{\"surname\":\"本\"},{\"surname\":\"性\"},{\"surname\":\"雪\"},{\"surname\":\"霜\"},{\"surname\":\"烟\"},{\"surname\":\"寒\"},{\"surname\":\"少\"},{\"surname\":\"字\"},{\"surname\":\"桥\"},{\"surname\":\"板\"},{\"surname\":\"斐\"},{\"surname\":\"独\"},{\"surname\":\"千\"},{\"surname\":\"诗\"},{\"surname\":\"嘉\"},{\"surname\":\"扬\"},{\"surname\":\"善\"},{\"surname\":\"揭\"},{\"surname\":\"祈\"},{\"surname\":\"析\"},{\"surname\":\"赤\"},{\"surname\":\"紫\"},{\"surname\":\"青\"},{\"surname\":\"柔\"},{\"surname\":\"刚\"},{\"surname\":\"奇\"},{\"surname\":\"拜\"},{\"surname\":\"佛\"},{\"surname\":\"陀\"},{\"surname\":\"弥\"},{\"surname\":\"阿\"},{\"surname\":\"素\"},{\"surname\":\"长\"},{\"surname\":\"僧\"},{\"surname\":\"隐\"},{\"surname\":\"仙\"},{\"surname\":\"隽\"},{\"surname\":\"宇\"},{\"surname\":\"祭\"},{\"surname\":\"酒\"},{\"surname\":\"淡\"},{\"surname\":\"塔\"},{\"surname\":\"琦\"},{\"surname\":\"闪\"},{\"surname\":\"始\"},{\"surname\":\"星\"},{\"surname\":\"南\"},{\"surname\":\"天\"},{\"surname\":\"接\"},{\"surname\":\"波\"},{\"surname\":\"碧\"},{\"surname\":\"速\"},{\"surname\":\"禚\"},{\"surname\":\"腾\"},{\"surname\":\"潮\"},{\"surname\":\"镜\"},{\"surname\":\"似\"},{\"surname\":\"澄\"},{\"surname\":\"潭\"},{\"surname\":\"謇\"},{\"surname\":\"纵\"},{\"surname\":\"渠\"},{\"surname\":\"奈\"},{\"surname\":\"风\"},{\"surname\":\"春\"},{\"surname\":\"濯\"},{\"surname\":\"沐\"},{\"surname\":\"茂\"},{\"surname\":\"英\"},{\"surname\":\"兰\"},{\"surname\":\"檀\"},{\"surname\":\"藤\"},{\"surname\":\"枝\"},{\"surname\":\"检\"},{\"surname\":\"生\"},{\"surname\":\"折\"},{\"surname\":\"登\"},{\"surname\":\"驹\"},{\"surname\":\"骑\"},{\"surname\":\"貊\"},{\"surname\":\"虎\"},{\"surname\":\"肥\"},{\"surname\":\"鹿\"},{\"surname\":\"雀\"},{\"surname\":\"野\"},{\"surname\":\"禽\"},{\"surname\":\"飞\"},{\"surname\":\"节\"},{\"surname\":\"宜\"},{\"surname\":\"鲜\"},{\"surname\":\"粟\"},{\"surname\":\"栗\"},{\"surname\":\"豆\"},{\"surname\":\"帛\"},{\"surname\":\"官\"},{\"surname\":\"布\"},{\"surname\":\"衣\"},{\"surname\":\"藏\"},{\"surname\":\"宝\"},{\"surname\":\"钞\"},{\"surname\":\"银\"},{\"surname\":\"门\"},{\"surname\":\"盈\"},{\"surname\":\"庆\"},{\"surname\":\"喜\"},{\"surname\":\"及\"},{\"surname\":\"普\"},{\"surname\":\"建\"},{\"surname\":\"曲\"},{\"surname\":\"竹\"},{\"surname\":\"百\"},{\"surname\":\"福\"},{\"surname\":\"言\"},{\"surname\":\"第五\"},{\"surname\":\"佟\"},{\"surname\":\"爱\"},{\"surname\":\"年\"},{\"surname\":\"笪\"},{\"surname\":\"谯\"},{\"surname\":\"哈\"},{\"surname\":\"墨\"},{\"surname\":\"南宫\"},{\"surname\":\"赏\"},{\"surname\":\"伯\"},{\"surname\":\"佴\"},{\"surname\":\"佘\"},{\"surname\":\"牟\"},{\"surname\":\"商\"},{\"surname\":\"西门\"},{\"surname\":\"东门\"},{\"surname\":\"左丘\"},{\"surname\":\"梁丘\"},{\"surname\":\"琴\"},{\"surname\":\"况\"},{\"surname\":\"亢\"},{\"surname\":\"缑\"},{\"surname\":\"帅\"},{\"surname\":\"微生\"},{\"surname\":\"羊舌\"},{\"surname\":\"海\"},{\"surname\":\"归\"},{\"surname\":\"呼延\"},{\"surname\":\"南门\"},{\"surname\":\"东郭\"},{\"surname\":\"百里\"},{\"surname\":\"钦\"},{\"surname\":\"鄢\"},{\"surname\":\"汝\"},{\"surname\":\"法\"},{\"surname\":\"闫\"},{\"surname\":\"楚\"},{\"surname\":\"晋\"},{\"surname\":\"谷梁\"},{\"surname\":\"宰父\"},{\"surname\":\"夹谷\"},{\"surname\":\"拓跋\"},{\"surname\":\"壤驷\"},{\"surname\":\"乐正\"},{\"surname\":\"漆雕\"},{\"surname\":\"公西\"},{\"surname\":\"巫马\"},{\"surname\":\"端木\"},{\"surname\":\"颛孙\"},{\"surname\":\"子车\"},{\"surname\":\"督\"},{\"surname\":\"仉\"},{\"surname\":\"司寇\"},{\"surname\":\"亓官\"},{\"surname\":\"汤 \"},{\"surname\":\"庞 \"},{\"surname\":\"牙\"},{\"surname\":\"翟

\"},{\"surname\":\"瞿 \"},{\"surname\":\"嫣\"},{\"surname\":\"陶\"}]}";

private NameInfoinfo;

private ListinfoList;

//装匹配到的姓氏

    private ListjsonName;

@Override

    protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

setListener();

runOnUiThread(new Runnable() {

@Override

            public void run() {

}

});

//        getDatasync();

    }

public void getDatasync(){

new Thread(new Runnable() {

@Override

            public void run() {

try {

OkHttpClient client =new OkHttpClient();//创建OkHttpClient对象

                    Request request =new Request.Builder()

.url("http://www.baidu.com")//请求接口。如果需要传参拼接到接口后面。

                            .build();//创建Request 对象

                    Response response =null;

response = client.newCall(request).execute();//得到Response 对象

                    if (response.isSuccessful()) {

Log.d("kwwl","response.code()=="+response.code());

Log.d("kwwl","response.message()=="+response.message());

Log.d("kwwl","res=="+response.body().string());

//此时的代码执行在子线程,修改UI的操作请使用handler跳转到UI线程。

                    }

}catch (Exception e) {

e.printStackTrace();

}

}

}).start();

}

private void initView() {

button1 = findViewById(R.id.button1);

button2 = findViewById(R.id.button2);

content = findViewById(R.id.et_content);

}

private void setListener() {

button1.setOnClickListener(this);

button2.setOnClickListener(this);

}

//将汉语转换成拼音

    public String changePinyin(String a) {

String pinyin ="";

char[] chars = a.toCharArray();

List list = list =new ArrayList<>();

for (int i =0; i < chars.length; i++) {

//-------------------指定格式转换----------------------------

            HanyuPinyinOutputFormat format =new HanyuPinyinOutputFormat();

//输出小写

            format.setCaseType(HanyuPinyinCaseType.LOWERCASE);

//无音标

            format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

// WITH_V:用v表示ü  (nv)

            format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);

try {

if (a.isEmpty()) {

Toast.makeText(MainActivity.this,"数据不能为空", Toast.LENGTH_SHORT).show();

}else {

//存储一个字的所有发音

                    String[] all = PinyinHelper.toHanyuPinyinStringArray(chars[i], format);

//多音字时默认取第一个发音

                    String text1 = all[0];

list.add(text1);

}

}catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {

badHanyuPinyinOutputFormatCombination.printStackTrace();

}

}

for (int i =0; i < chars.length; i++) {

pinyin += list.get(i);

}

return pinyin;

}

//姓氏拼音

    public String xingPinyin(String a) {

char[] b = a.toCharArray();

return changePinyin(String.valueOf(b[0]));

}

//名字拼音

    public String mingPinyin(String a) {

String b = a.substring(1, a.length());

return changePinyin(b);

}

//姓氏与名字的拆分

    private String getFirstName(String str, String json) {

infoList =new ArrayList();

jsonName =new ArrayList();

Gson gson =new Gson();

//姓氏存在于姓氏表与否

        String surname ="";

JsonParser parser =new JsonParser();

JsonObject object = parser.parse(json).getAsJsonObject();

JsonArray array = object.get("RECORDS").getAsJsonArray();

for (JsonElement element : array) {

// 遍历取出数组里的每个element, 此处就跟把一个把简单json字符串转成对象一样了

            info = gson.fromJson(element, NameInfo.class);

infoList.add(info);

}

for (int i =0; i

if (str.contains(infoList.get(i).getSurname())) {

char[] name = str.toCharArray();

String firtFont = String.valueOf(name[0]);

if (infoList.get(i).getSurname().contains(firtFont)) {

jsonName.add(infoList.get(i).getSurname());

surname =jsonName.get(0);

Log.i("jsonName",surname);

return surname;

}else {

surname ="";

}

}

}

//        for (int i = 0; i < jsonName.size(); i++) {

//            System.out.println(str + " 的姓氏为: " + jsonName.get(i));

//        }

        System.out.println(str +" 的姓氏为: " + surname);

return surname;

}

@Override

    public void onClick(View view) {

if (view.getId() == R.id.button1) {

text = String.valueOf(content.getText());

if (text.isEmpty()) {

Toast.makeText(MainActivity.this,"数据不能为空", Toast.LENGTH_SHORT).show();

}else {

//                String b=text.substring(0,1);

//                String tr1=text.replaceAll(" ","");

//判断姓氏是否存在于姓氏表,没有即默认第一个汉字为姓

                if (!getFirstName(text,json).isEmpty()) {

Log.i("key2", getFirstName(text,json));

button1.setText(changePinyin(getFirstName(text,json)));

}else {

String b =text.substring(0,1);

System.out.println(getFirstName(text,json));

button1.setText(changePinyin(b));

}

}

}

if (view.getId() == R.id.button2) {

text = String.valueOf(content.getText());

if (text.isEmpty()) {

Toast.makeText(MainActivity.this,"数据不能少于两个字", Toast.LENGTH_SHORT).show();

}else {

String a = getFirstName(text,json);

if (!a.isEmpty()) {

char[] chars = a.toCharArray();

//                String tr1=text.replaceAll(" ","");

                    String b =text.substring(chars.length,text.length());

Log.i("key3", b);

button2.setText(changePinyin(b));

}else {

String b =text.substring(1,text.length());

button2.setText(changePinyin(b));

}

}

}

}

}

另提供布局文件:

<?xml version="1.0" encoding="utf-8"?>

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context=".MainActivity">

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/et_content"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintLeft_toLeftOf="parent"

        app:layout_constraintRight_toRightOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/button1"

        android:textAllCaps="false"/>

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/button2"

        android:textAllCaps="false" />

由于无法确定多音字的读音,还有些姓氏表不完整,姓氏表没有的话,取第一个字为姓,复姓也有可能检索出来比第一个字慢出现,就可能取第一个字为姓氏了,方法只能尽量保证翻译的准确,不能保证百分百,请大家见谅。

由于是第一次写简书,写作布局弄的好难看,大家见谅。当然最关键的是我要下班了,哈哈,随便弄弄的

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,864评论 6 494
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,175评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,401评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,170评论 1 286
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,276评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,364评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,401评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,179评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,604评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,902评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,070评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,751评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,380评论 3 319
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,077评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,312评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,924评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,957评论 2 351

推荐阅读更多精彩内容