我知道这是一个愚蠢的问题,因为我是新的,我正在做笔记应用程序,我添加了语音识别功能,只需将文本添加到textview中,无论我们说什么。它是完美的工作,但问题是,当我使用相同的第二次以前的文本被删除,并获得新的文本。我需要让旧文本像这样,然后添加新文本。
This is my speech to text code
layoutMiscellaneous.findViewById(R.id.speechtotext).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"Hi Speak Something");
try {
startActivityForResult(intent, 33333);
} catch (ActivityNotFoundException e) {
Toast.makeText(CreateNoteActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_SELECT_IMAGE && resultCode == RESULT_OK) {
if (data != null) {
Uri selectedImageUri = data.getData();
if(selectedImageUri != null) {
try {
InputStream inputStream = getContentResolver().openInputStream(selectedImageUri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
imageNote.setImageBitmap(bitmap);
imageNote.setVisibility(View.VISIBLE);
findViewById(R.id.imageRemoveImage).setVisibility(View.VISIBLE);
selectedImagePath = getPathFromUri(selectedImageUri);
} catch (Exception exception) {
Toast.makeText(this, exception.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
}
if (requestCode == 33333 && resultCode == RESULT_OK && null != data){
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
inputNoteText.setText(result.get(0));
}
}
这一行:
inputNoteText.setText(result.get(0));
它只是重置文本,然后将其替换为您的输入。
你所要做的就是把已经在里面的文本存储起来,然后追加输入。
像这样:
inputNoteText.setText(inputNoteText.getText()+result.get(0));
新的完整代码:
//This is my speech to text code
layoutMiscellaneous.findViewById(R.id.speechtotext).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"Hi Speak Something");
try {
startActivityForResult(intent, 33333);
} catch (ActivityNotFoundException e) {
Toast.makeText(CreateNoteActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_SELECT_IMAGE && resultCode == RESULT_OK) {
if (data != null) {
Uri selectedImageUri = data.getData();
if(selectedImageUri != null) {
try {
InputStream inputStream = getContentResolver().openInputStream(selectedImageUri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
imageNote.setImageBitmap(bitmap);
imageNote.setVisibility(View.VISIBLE);
findViewById(R.id.imageRemoveImage).setVisibility(View.VISIBLE);
selectedImagePath = getPathFromUri(selectedImageUri);
} catch (Exception exception) {
Toast.makeText(this, exception.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
}
if (requestCode == 33333 && resultCode == RESULT_OK && null != data){
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
inputNoteText.setText(inputNoteText.getText()+result.get(0));
}
}