语音识别器在编辑文本错误中设置文本(Android 工作室)
speech recognizer set text in edit text mishap (Android studio)
我正在使用语音识别器使用语音到文本的概念,但是当文本设置为编辑文本时,编辑文本中的现有文本将被删除,并设置从语音转换的文本。
但是,我希望将转换后的文本设置在现有文本之后,而不替换现有文本。
public class AddNoteActivity extends AppCompatActivity {
public static final int RECORDAUDIOREQUESTCODE = 1;
private SpeechRecognizer speechRecognizer;
ImageView micButton;
public static final String EXTRA_ID = "EXTRA_ID";
public static final String EXTRA_TITLE =
"EXTRA_TITLE";
public static final String EXTRA_DESCRIPTION =
"EXTRA_DESCRIPTION";
private EditText editTextTitle;
private EditText editTextDescription;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_note);
if(ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)!=
PackageManager.PERMISSION_GRANTED){
checkPermission();
}
editTextTitle = findViewById(R.id.edit_text_title);
editTextDescription = findViewById(R.id.edit_text_description);
Objects.requireNonNull(getSupportActionBar()).setHomeAsUpIndicator(R.drawable.ic_baseline_close_24);
micButton = findViewById(R.id.mic);
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
final Intent speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
speechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle params) {
}
@Override
public void onBeginningOfSpeech() {
editTextDescription.setSelection(editTextDescription.getText().length());
}
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
editTextDescription.setSelection(editTextDescription.getText().length());
}
@Override
public void onError(int error) {
}
@Override
public void onResults(Bundle results) {
micButton.setImageResource(R.drawable.ic_baseline_mic_off_24);
ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
editTextDescription.setSelection(editTextDescription.getText().length());
editTextDescription.setText(data.get(0));
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onEvent(int eventType, Bundle params) {
}
});
micButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
micButton.setImageResource(R.drawable.ic_baseline_mic_24);
speechRecognizer.startListening(speechRecognizerIntent);
}
});
@Override
protected void onDestroy(){
super.onDestroy();
speechRecognizer.destroy();
}
private void checkPermission(){
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.RECORD_AUDIO},
RECORDAUDIOREQUESTCODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
super.onRequestPermissionsResult(requestCode,permissions,grantResults);
if(requestCode==RECORDAUDIOREQUESTCODE && grantResults.length>0){
if(grantResults[0]==PackageManager.PERMISSION_GRANTED)
Toast.makeText(this, "Permission Granted", Toast.LENGTH_LONG).show();
}
}
@Override
public void onResults(Bundle results) {
micButton.setImageResource(R.drawable.ic_baseline_mic_off_24);
ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
editTextDescription.setSelection(editTextDescription.getText().length());
editTextDescription.setText(data.get(0));
}
你告诉它突出显示整个文本,然后设置文本。这将摆脱任何选择(它也是多余的,因为如果您没有设置选择也会发生同样的事情)。如果要继续,请使用
editTextDescription.setText(editTextDescription.getText().toString()+data.get(0))
应该替换该函数的最后两行。它将获取现有文本,将新结果附加到它,并将总字符串设置到视图。
我正在使用语音识别器使用语音到文本的概念,但是当文本设置为编辑文本时,编辑文本中的现有文本将被删除,并设置从语音转换的文本。
public class AddNoteActivity extends AppCompatActivity {
public static final int RECORDAUDIOREQUESTCODE = 1;
private SpeechRecognizer speechRecognizer;
ImageView micButton;
public static final String EXTRA_ID = "EXTRA_ID";
public static final String EXTRA_TITLE =
"EXTRA_TITLE";
public static final String EXTRA_DESCRIPTION =
"EXTRA_DESCRIPTION";
private EditText editTextTitle;
private EditText editTextDescription;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_note);
if(ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)!=
PackageManager.PERMISSION_GRANTED){
checkPermission();
}
editTextTitle = findViewById(R.id.edit_text_title);
editTextDescription = findViewById(R.id.edit_text_description);
Objects.requireNonNull(getSupportActionBar()).setHomeAsUpIndicator(R.drawable.ic_baseline_close_24);
micButton = findViewById(R.id.mic);
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
final Intent speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
speechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle params) {
}
@Override
public void onBeginningOfSpeech() {
editTextDescription.setSelection(editTextDescription.getText().length());
}
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
editTextDescription.setSelection(editTextDescription.getText().length());
}
@Override
public void onError(int error) {
}
@Override
public void onResults(Bundle results) {
micButton.setImageResource(R.drawable.ic_baseline_mic_off_24);
ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
editTextDescription.setSelection(editTextDescription.getText().length());
editTextDescription.setText(data.get(0));
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onEvent(int eventType, Bundle params) {
}
});
micButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
micButton.setImageResource(R.drawable.ic_baseline_mic_24);
speechRecognizer.startListening(speechRecognizerIntent);
}
});
@Override
protected void onDestroy(){
super.onDestroy();
speechRecognizer.destroy();
}
private void checkPermission(){
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.RECORD_AUDIO},
RECORDAUDIOREQUESTCODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
super.onRequestPermissionsResult(requestCode,permissions,grantResults);
if(requestCode==RECORDAUDIOREQUESTCODE && grantResults.length>0){
if(grantResults[0]==PackageManager.PERMISSION_GRANTED)
Toast.makeText(this, "Permission Granted", Toast.LENGTH_LONG).show();
}
}
@Override
public void onResults(Bundle results) {
micButton.setImageResource(R.drawable.ic_baseline_mic_off_24);
ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
editTextDescription.setSelection(editTextDescription.getText().length());
editTextDescription.setText(data.get(0));
}
你告诉它突出显示整个文本,然后设置文本。这将摆脱任何选择(它也是多余的,因为如果您没有设置选择也会发生同样的事情)。如果要继续,请使用
editTextDescription.setText(editTextDescription.getText().toString()+data.get(0))
应该替换该函数的最后两行。它将获取现有文本,将新结果附加到它,并将总字符串设置到视图。