Android 开发汉字转带声调的拼音

复制代码
一、引入:
implementation("com.github.stuxuhai:jpinyin:1.1.7")

二、布局

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="16dp">
    <TextView
        android:id="@+id/textViewPinyin"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/edittext_background" />

    <EditText
        android:id="@+id/editTextPinyin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入文字" />
    <Button
        android:id="@+id/btnSpeak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="朗读" />

</LinearLayout>

三、java 代码

复制代码
package com.wzt.lovestudy;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;

import java.util.Locale;
public class StudyActivity extends Activity {
    private TextToSpeech tts;

    private Button btnSpeak;

    private EditText editTextPinyin;
    private TextView textViewPinyin; // 假设


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initClick();
        initTTS();
        addTextChangedListener();

    }

    private void initView() {

        btnSpeak = findViewById(R.id.btnSpeak);
        editTextPinyin = findViewById(R.id.editTextPinyin);
        textViewPinyin = findViewById(R.id.textViewPinyin);
    }

    private void initClick() {
        btnSpeak.setOnClickListener(v -> {
            String text = editTextPinyin.getText().toString();
            if (text.isEmpty()) {
                Toast.makeText(StudyActivity.this, "请输入文本", Toast.LENGTH_SHORT).show();
                return;
            }
            // 执行朗读
            tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
        });
    }

    private void addTextChangedListener() {
        editTextPinyin.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String result ;
                try {
                    result = PinyinHelper.convertToPinyinString(s.toString(), " ", PinyinFormat.WITH_TONE_MARK);
                } catch (PinyinException e) {
                    throw new RuntimeException(e);
                }
                textViewPinyin.setText(result);

            }
        });
    }


    private void initTTS() {
        tts = new TextToSpeech(this, status -> {
            if (status == TextToSpeech.SUCCESS) {
                // 设置语言为中文
                int result = tts.setLanguage(Locale.CHINA);
                if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("TTS", "当前设备不支持中文朗读");
                    Toast.makeText(StudyActivity.this, "不支持中文朗读", Toast.LENGTH_SHORT).show();
                } else {
                    // 可选:调整语速和音调
                    tts.setSpeechRate(1.0f);
                    tts.setPitch(1.0f);
                    Log.e("TTS", "TTS 初始化成功");
                }
            } else {
                Log.e("TTS", "TTS 初始化失败");
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }
}
相关推荐
Purple Coder11 小时前
BMS学习经验
学习
Tigshop开源商城11 小时前
『订单税率+收货地址校验国家字段』功能上新|跨境运营更高效,Tigshop开源商城系统 JAVA v5.8.23 版本更新
java·开源商城系统·tigshop
REDcker11 小时前
C++变量存储与ELF段布局详解 从const全局到rodata与nm_readelf验证实践
java·c++·面试
大炮筒11 小时前
COCOS2DX4.0CPPWIN移植安卓踩坑总结
android
经济元宇宙11 小时前
摄影培训行业百科:机构选择与学习路径全解析
大数据·人工智能·学习
じ☆冷颜〃12 小时前
实分析与测度论、复分析、傅里叶分析、泛函分析、凸分析概述.
笔记·学习·数学建模·拓扑学·傅立叶分析
星夜夏空9912 小时前
STM32单片机学习(10)——GPIO输入
stm32·单片机·学习
kobesdu12 小时前
【ROS2实战笔记-19】ROS2 生命周期节点的启动顺序、状态转换陷阱与热备方案
java·前端·笔记·机器人·ros·ros2
neo_Ggx2312 小时前
Maven 版本管理详解:SNAPSHOT、Release 与 Nexus 仓库的区别和影响
java·maven
matlabgoodboy12 小时前
软件开发定制小程序APP帮代做java代码代编写C语言设计python编程
java·c语言·小程序