我有一个ScrollView,它具有大约13个EditText。我正在尝试将语音转换为文本,因此,当用户触摸任何EditText并单击“讲话”按钮并开始讲话时,它将转换为该EditText的文本,而当他再次触摸另一个EditText时,它将转换为该文本。我使用了循环,但在所有13个EditText中都发现了一个语音的结果。我还添加了一个break语句,但效果不佳。
public void onResults(Bundle bundle) {
//getting all the matches
ArrayList<String> matches = bundle
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (matches != null) {
for (int j = 0; j < speEdtId.length; j++) {
eEdit = findViewById(speEdtId[j]);
if (speEdtId[j]==speEdtId[0]) {
eEdit.setText(matches.get(0));
} else if (speEdtId[j]==speEdtId[1]) {
eEdit.setText(matches.get(0));
} else if (speEdtId[j]==speEdtId[2]) {
eEdit.setText(matches.get(0));}
.
.
.
break;
}
}}
答案 0 :(得分:0)
您正在为每个Edittext设置相同的值。您可以做的是:
if (speEdtId[j]==speEdtId[0]) {
eEdit.setText(matches.get(0));
matches.clear();
}
然后还将标记设置为每个Edittext,并在循环内使用标记查找。希望这会有所帮助
答案 1 :(得分:0)
似乎您将有很多Speech EditText,但我不确定您希望程序如何与EditText一起使用,因此在解决方案中将使用TextView。我认为,使用RecyclerViews比手动创建13个EditText更好,更有效。
首先,我将创建一个Speech对象。您可以添加任意数量的变量,例如语音的时间戳记
Speech.java
public class Speech {
private String speechInText;
private String speech;
public Speech(){
}
public Speech(String speechInText, String speech) {
this.speechInText = speechInText;
this.speech = speech;
}
public String getSpeechInText() {
return speechInText;
}
public void setSpeechInText(String speechInText) {
this.speechInText = speechInText;
}
public String getSpeech() {
return speech;
}
public void setSpeech(String speech) {
this.speech = speech;
}
}
然后,我将为每个演讲创建一个适配器
SpeechAdapter.java
public class SpeechAdapter extends RecyclerView.Adapter<SpeechAdapter.ViewHolder> {
public static final String TAG = SpeechAdapter.class.getName();
public interface OnItemClickListener {
void onItemClick(Speech speech, int pos);
}
private List<Speech> speeches;
private AppCompatActivity parentActivity;
private final OnItemClickListener listener;
public SpeechAdapter(AppCompatActivity parentActivity, List<Speech> speeches, OnItemClickListener listener) {
this.parentActivity = parentActivity;
this.speeches = speeches;
this.listener = listener;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView speechInTextTV;
public ViewHolder(View v) {
super(v);
parentActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
speechInTextTV = itemView.findViewById(R.id.speechInTextTV);
}
});
}
public void bind(final Speech speech,final int pos, final OnItemClickListener listener) {
itemView.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
listener.onItemClick(speech, pos);
}
});
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.adapter_speech_list, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int pos) {
final Speech speech = speeches.get(pos);
holder.speechInTextTV.setText(speech.getSpeechInText());
holder.bind(speech, pos, listener);
}
@Override
public int getItemCount() {
return speeches.size();
}
}
接下来,我将为适配器创建一个XML文件
adapter_speech_list.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/white">
<TextView
android:id="@+id/speechInTextTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:padding="20dp"/>
</android.support.constraint.ConstraintLayout>
最后,活动文件或片段文件
SpeechActivity.java
public class SpeechActivity extends AppCompatActivity{
public static final String TAG = SpeechActivity.class.getName();
private SpeechAdapter speechAdapter;
private List<Speech> speeches = new ArrayList<>();
private RecyclerView speechRV;
private int selectedSpeechPosition;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speech);
speechRV = findViewById(R.id.speechRV);
//I add 13 text views manually here (with empty data)
for(int i = 0; i < 13; i++){
speeches.add(new Speech("",""));
}
speechAdapter = new SpeechAdapter(this, speeches, new SpeechAdapter.OnItemClickListener() {
@Override
public void onItemClick(Speech speech, int position) {
/*
you convert speech to text here
*/
selectedSpeechPosition = position;
}
});
speechRV.setNestedScrollingEnabled(false);
speechRV.setAdapter(speechAdapter);
speechRV.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
}
public void onResults(Bundle bundle) {
//getting all the matches
ArrayList<String> matches = bundle
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
/*here you get the result of the converted speech and update the speeches list and then update the adapter to refresh the view*/
String convertedSpeech = "<PUT_YOUR_RESULT_HERE>";
speeches.set(selectedSpeechPosition,new Speech(convertedSpeech,"user's original speech or something else?"));
speechAdapter.notifyDataSetChanged();
}
}
最后是活动的xml文件
activity_speech.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/white"
android:clickable="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/speechRV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</android.support.constraint.ConstraintLayout>
此解决方案仅是示例,如果我在您的位置,我的解决方法也是如此。您应该学习代码并自己进行操作以满足自己的要求。
基本上,您应该将某种类型的数据传递到语音列表中,并在onResult函数中更新适配器,然后将使用新数据刷新回收站视图。当我在活动文件中初始化适配器时,我已经实现了OnItemClick函数,因此当用户在“回收者”视图中单击某个项目时,它将保存所选语音的位置,以便将其保存在onResult函数中,您可以知道自己是哪种语音应该更新(通过使用selectedSpeechPosition)