如何使用Java在 Android 中构建一个简单的语音打字机应用程序?
先决条件:
- 面向初学者的 Android 应用开发基础知识
- 安装和设置 Android Studio 指南
- 如何在 Android Studio 中创建/启动新项目?
- 运行您的第一个 Android 应用程序
- Android 中的微调器
- Android中的识别器意图
在本文中,我们将使用Java在 Android 中构建一个简单的Voice Typer 应用程序。这是我们将要构建的应用程序的一瞥。该应用程序包含一个带有两个 TextView 的 Activity,一个用于选择语言的 Spinner,一个用于显示消息的 EditText,以及两个可单击的 ImageView,一个用于提供语音输入,另一个用于将消息复制到剪贴板。我们将使用 RecognizerIntent 类来获取用户的语音输入。下面给出了一个示例视频,以了解我们将在本文中做什么。
预览:
分步实施
第 1 步:创建一个新项目
要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择Java作为编程语言。
第 2 步:添加矢量资源
让我们添加必要的矢量资源和可绘制资源文件。转到app > res > drawable并添加以下 XML 文件。
ic_mic.xml (麦克风图标)
XML
XML
XML
-
-
XML
Java
package com.cs.voicetyper;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
EditText etText;
ImageView ivMic,ivCopy;
Spinner spLangs;
String lcode = "en-US";
// Languages included
String[] languages = {"English","Tamil","Hindi","Spanish","French",
"Arabic","Chinese","Japanese","German"};
// Language codes
String[] lCodes = {"en-US","ta-IN","hi-IN","es-CL","fr-FR",
"ar-SA","zh-TW","jp-JP","de-DE"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize views
etText = findViewById(R.id.etSpeech);
ivMic = findViewById(R.id.ivSpeak);
ivCopy = findViewById(R.id.ivCopy);
spLangs = findViewById(R.id.spLang);
// set onSelectedItemListener for the spinner
spLangs.setOnItemSelectedListener(this);
// setting array adapter for spinner
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item,languages);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spLangs.setAdapter(adapter);
// on click listener for mic icon
ivMic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating intent using RecognizerIntent to convert speech to text
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,lcode);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Speak now!");
// starting intent for result
activityResultLauncher.launch(intent);
}
});
// on click listener to copy the speech
ivCopy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// code to copy to clipboard
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboardManager.setPrimaryClip(ClipData.newPlainText("label",etText.getText().toString().trim()));
Toast.makeText(MainActivity.this, "Copied!", Toast.LENGTH_SHORT).show();
}
});
}
// activity result launcher to start intent
ActivityResultLauncher activityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback() {
@Override
public void onActivityResult(ActivityResult result) {
// if result is not empty
if (result.getResultCode() == Activity.RESULT_OK && result.getData()!=null) {
// get data and append it to editText
ArrayList d=result.getData().getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
etText.setText(etText.getText()+" "+d.get(0));
}
}
});
@Override
public void onItemSelected(AdapterView> adapterView, View view, int i, long l) {
// setting lcode corresponding
// to the language selected
lcode = lCodes[i];
}
@Override
public void onNothingSelected(AdapterView> adapterView) {
// automatically generated method
// for implementing onItemSelectedListener
}
}
预览:
ic_copy.xml (剪贴板图标)
XML
预览:
et_bg.xml (EditText 的自定义背景)
XML
-
-
预览:
这是一个截图供参考。
第 3 步:使用 activity_main.xml 文件
我们已经为我们正在构建的应用程序添加了必要的资源文件。现在,让我们为我们的应用程序设计 UI。将此布局资源文件添加到app > res > layout 。下面是activity_main.xml文件的代码。
XML
预览:
第 4 步:使用 MainActivity。Java
现在是时候初始化MainActivity中的所有内容并实现 Spinner 和 RecognizerIntent。这是 MainActivity 的完整代码。下面是MainActivity 的代码。 Java文件。代码中添加了注释以更详细地理解代码。
Java
package com.cs.voicetyper;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
EditText etText;
ImageView ivMic,ivCopy;
Spinner spLangs;
String lcode = "en-US";
// Languages included
String[] languages = {"English","Tamil","Hindi","Spanish","French",
"Arabic","Chinese","Japanese","German"};
// Language codes
String[] lCodes = {"en-US","ta-IN","hi-IN","es-CL","fr-FR",
"ar-SA","zh-TW","jp-JP","de-DE"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize views
etText = findViewById(R.id.etSpeech);
ivMic = findViewById(R.id.ivSpeak);
ivCopy = findViewById(R.id.ivCopy);
spLangs = findViewById(R.id.spLang);
// set onSelectedItemListener for the spinner
spLangs.setOnItemSelectedListener(this);
// setting array adapter for spinner
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item,languages);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spLangs.setAdapter(adapter);
// on click listener for mic icon
ivMic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating intent using RecognizerIntent to convert speech to text
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,lcode);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Speak now!");
// starting intent for result
activityResultLauncher.launch(intent);
}
});
// on click listener to copy the speech
ivCopy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// code to copy to clipboard
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboardManager.setPrimaryClip(ClipData.newPlainText("label",etText.getText().toString().trim()));
Toast.makeText(MainActivity.this, "Copied!", Toast.LENGTH_SHORT).show();
}
});
}
// activity result launcher to start intent
ActivityResultLauncher activityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback() {
@Override
public void onActivityResult(ActivityResult result) {
// if result is not empty
if (result.getResultCode() == Activity.RESULT_OK && result.getData()!=null) {
// get data and append it to editText
ArrayList d=result.getData().getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
etText.setText(etText.getText()+" "+d.get(0));
}
}
});
@Override
public void onItemSelected(AdapterView> adapterView, View view, int i, long l) {
// setting lcode corresponding
// to the language selected
lcode = lCodes[i];
}
@Override
public void onNothingSelected(AdapterView> adapterView) {
// automatically generated method
// for implementing onItemSelectedListener
}
}
就是这样。现在我们可以运行应用程序了。确保您的项目包含以下所有文件。
这是最终应用程序的预览。
输出: