我是Android Studio的新手。我尝试用一种编辑文本尝试“语音转换为文本”,并且效果很好。但是我想用一个按钮将语音应用于文本以编辑两个文本。有人可以帮我吗?我尝试了其他资源,但是没有用。
<EditText
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:hint="phone"
android:ems="10" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="text"
android:ems="10"
android:inputType="text" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
这是我要通过单个按钮和两个编辑文本将语音应用于文本的Java代码。单击该按钮时,它应该向下滚动到下一个编辑文本并接受输入
public class MainActivity extends AppCompatActivity {
EditText tv1;
EditText tv2;
Button btn;
private static final int REQUEST_CODE_SPEECH_INPUT = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = findViewById(R.id.txt);
tv2=findViewById(R.id.editText);
btn = findViewById(R.id.button3);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
speak();
}
private void speak() {
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, "say something");
try {
startActivityForResult(intent, REQUEST_CODE_SPEECH_INPUT);
} catch (Exception e) {
///Toast.makeText(this,"",Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
tv1.setText(result.get(0));
}
break;
}
}
}
}
答案 0 :(得分:0)
也许您可以尝试
9605.795975874069
9605.795975874067
9605.79597587407
答案 1 :(得分:0)
onCreate之前:
private boolean isFirst=true;
在onActivityResult内部:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
if(isFirst){
isFirst=false;
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
tv1.setText(result.get(0));
//startAnother ActivityFor Result here but getting the second edittext
}else{
isFirst=true;
}
}
break;
}
}
}