如何在Android中加载说话时显示进度对话框?

时间:2018-06-02 23:06:06

标签: android

我有一个按钮,可以说出超过4000个字符的文字。它也正常工作。但是当我点击按钮时,它需要时间来说话。所以我想创建一个progres Dialog所以,当我按下speak按钮时,它会显示进度对话框,然后在它开始讲话时解除它。

  private void loadSpeaking(String textForReading) 
      {
        int dividerLimit = 3900;
        if(textForReading.length() >= dividerLimit) {
         int textLength = textForReading.length();
         ArrayList<String> texts = new ArrayList<String>();
         int count = textLength / dividerLimit + ((textLength % dividerLimit == 0) ? 0 : 1);
         int start = 0;
        int end = textForReading.indexOf(" ", dividerLimit);
        for(int i = 1; i<=count; i++) {
        texts.add(textForReading.substring(start, end));
        start = end;
        if((start + dividerLimit) < textLength) {
        end = textForReading.indexOf(" ", start + dividerLimit);
        } 
      else {
         end = textLength;
           }
    }
     for(int i=0; i<texts.size(); i++) {
       textToSpeech.speak(texts.get(i), TextToSpeech.QUEUE_ADD, null);
      }
     } else {
   textToSpeech.speak(textForReading, TextToSpeech.QUEUE_FLUSH, null);
}
}

1 个答案:

答案 0 :(得分:0)

为什么不使用简单的ProgressDialog?

试试这个:

private ProgressDialog dialog;
private void loadSpeaking(String textForReading) 
  {
    dialog.setMessage("Please wait");
    dialog.setCancelable(false);
    dialog.setCanceledOnTouchOutside(false);
    dialog.show();
    int dividerLimit = 3900;
    if(textForReading.length() >= dividerLimit) {
     int textLength = textForReading.length();
     ArrayList<String> texts = new ArrayList<String>();
     int count = textLength / dividerLimit + ((textLength % dividerLimit == 0) ? 0 : 1);
     int start = 0;
    int end = textForReading.indexOf(" ", dividerLimit);
    for(int i = 1; i<=count; i++) {
    texts.add(textForReading.substring(start, end));
    start = end;
    if((start + dividerLimit) < textLength) {
    end = textForReading.indexOf(" ", start + dividerLimit);
    } 
  else {
     end = textLength;
       }
}
 for(int i=0; i<texts.size(); i++) {
   textToSpeech.speak(texts.get(i), TextToSpeech.QUEUE_ADD, null);
  }
 } else {
   textToSpeech.speak(textForReading, TextToSpeech.QUEUE_FLUSH, null);
}
    if (dialog.isShowing())
        dialog.dismiss();
}