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();
    }
}
相关推荐
小bo波3 小时前
使用Thread子类创建线程 VS 使用Runnable接口创建线程的区别
java·多线程·thread·并发编程·runnable
SamDeepThinking3 小时前
高并发场景下,CompletableFuture与ForkJoinPool该如何取舍?
java·后端·面试
方白羽6 小时前
Android Gradle 缓存与文件目录深度解析
android·gradle·android studio
张不才6 小时前
CPU 100% 了怎么办?Java 性能排障的标准化操作
java·后端
shepherd1118 小时前
吞吐量提升 10 倍:高并发大批量数据处理任务的架构演进与性能调优
java·后端·架构
曲幽10 小时前
Termux里的二进制和脚本,到底怎么运行才不踩坑?Termux-service 保活妙招!
android·termux·nohup·services·wake-lock
plainGeekDev11 小时前
单例模式 → object 声明
android·java·kotlin
程序员陆业聪11 小时前
读者点单·03|Compose 与传统 View 混用的 12 个真实坑
android
程序员陆业聪11 小时前
读者点单·02|Android 启动优化实战:Trace 抓取→Application 编排→冷启动全流程拆解
android
Coffeeee11 小时前
帮你快速理解AI Agent之我想招个Android实习生
android·人工智能·agent