这是我的代码
public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
String s = null;
Button btn=null;
Button b_prev=null;
s = ((Button) v).getText().toString().trim();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
b_prev=(Button)v;
//while(tts.isSpeaking());
tts.speak(s, TextToSpeech.QUEUE_FLUSH, null);
break;
case MotionEvent.ACTION_UP:
for(int i=0;i<mybtn.length;i++)
{
if(isPointInsideView(event.getRawX(),event.getRawY(),mybtn[i]))
{
s = (mybtn[i]).getText().toString().trim();
btn=mybtn[i];
break;
//et.setText("up android" + s, TextView.BufferType.EDITABLE);
}
}
if(btn.equals(mybtn[27])) //clear button pressed
{
s = et.getText().toString().trim();
int l = s.length();
if (l > 0) {
String t,p;
if (l > 1)
{
t = s.substring(0, l-1);
}
else
t = "";
p=s.substring(l-1, l);
p.trim();
et.setText(t, TextView.BufferType.EDITABLE );
//while(tts.isSpeaking());
tts.stop();
tts.speak("ok"+p,TextToSpeech.QUEUE_FLUSH , null);
while(tts.isSpeaking());
tts.speak("deleted",TextToSpeech.QUEUE_FLUSH , null);
//Toast.makeText(TimerActivity.this,p+"deleted",Toast.LENGTH_LONG).show();
}
}
else if(btn.equals(mybtn[26])) //space button
{
//s = et.getText().toString().trim();
et.setText(et.getText() + " ",TextView.BufferType.EDITABLE );
tts.stop();
tts.speak("OK space entered",TextToSpeech.QUEUE_FLUSH , null);
//get_Number(s);
}
else
{
//String s1= et.getText().toString().trim();
et.setText(et.getText() + "" + s,TextView.BufferType.EDITABLE );
//while(tts.isSpeaking());
tts.stop();
tts.speak("ok"+s,TextToSpeech.QUEUE_FLUSH , null);
while(tts.isSpeaking());
tts.speak("entered",TextToSpeech.QUEUE_FLUSH , null);
}
//et.setText("up android" + s, TextView.BufferType.EDITABLE);
//return true;
while(tts.isSpeaking());
//fun(true);
break;
case MotionEvent.ACTION_MOVE:
//if (current != Integer.valueOf(s)) {
//current = Integer.valueOf(s);
//}
//System.out.println("move android"+s);
for(int i=0;i<mybtn.length;i++)
{
if(isPointInsideView(event.getRawX(),event.getRawY(),mybtn[i]))
{
s = (mybtn[i]).getText().toString().trim();
btn=mybtn[i];
break;
//et.setText("down android" + s, TextView.BufferType.EDITABLE);
}
}
//while(tts.isSpeaking());
tts.stop();
if(btn!=b_prev)
{
//if(b_prev!=null)
System.out.println(b_prev.getText().toString()+" "+btn.getText().toString());
tts.speak(s, TextToSpeech.QUEUE_FLUSH, null);
}
b_prev=btn;
//finish();
//return false;
//fun(true);
break;
}
return true;
}
问题在于btn和b_prev按钮。当用户在键盘上滑动时,应该说出当前正在触摸的按钮。它正在发生并且运行良好,但是如果用户在同一个按钮内缓慢移动,则发出不应该发生的字母,这是因为每次TOUCH事件发生时b_prev
都被设置为NULL。
为了避免这个问题,我删除了这两个变量并在我的类中将它们声明为全局变量。使用以下行,我在logcat中获得了预期的结果。这也表明TTS引擎正确接收各种字母。
System.out.println(b_prev.getText().toString()+" "+btn.getText().toString());
然而,它并没有始终如一地说这些字母,而是有时说话而不是说话。但在局部变量的情况下,它曾经运作良好。我无法弄清楚问题并进行调试,因为代码没有变化。一切都一样,没有错误。
我认为问题与同步TTS和触摸事件有关,因为我必须使用各种TTS功能。请详细说明一下。我想要一些关于使用TTS来处理这些同步问题的好建议。请帮我。提前谢谢。