3

我正在創建一個應用程序,一個接一個地連續詢問用戶一系列問題。然而。我問的最後一個問題是「信息是否正確?」多個onClicks後延遲一個意圖?

我需要此提示以便在其他一系列連續問題出現後至少3000毫秒,以便用戶首先查看記錄的信息並查看它是否全部正確。但是目前,無論使用處理程序,最後的提示都會在第二個問題後立即出現。在出現最終提示之前是否有延遲至少3000毫秒的方法?

下面是我所指的當前代碼的一部分。請注意我已經在onClick以及onActivityResult部分嘗試過處理程序。

如果有人可以提供正確的位代碼,將不勝感激。

public void onClick(View v){ 

new Handler().postDelayed(new Runnable() { 
    @Override 
    public void run() { 
     Intent i1 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     i1.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
     i1.putExtra(RecognizerIntent.EXTRA_PROMPT, "Is the information correct? (Yes/No)"); 
     startActivityForResult(i1, check); 
    } 
}, 3000); 


Intent i2 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
i2.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
i2.putExtra(RecognizerIntent.EXTRA_PROMPT, "What is the current time?"); 
startActivityForResult(i2, checklv1); 

Intent i3 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
i3.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
i3.putExtra(RecognizerIntent.EXTRA_PROMPT, "Is the status Confirmed or Unconfirmed?"); 
startActivityForResult(i3, checklv2); 

Intent i4 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
i4.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
i4.putExtra(RecognizerIntent.EXTRA_PROMPT, "What is the temp?"); 
startActivityForResult(i4, checklv3); 

} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

if (requestCode == check && resultCode == RESULT_OK){ 
    ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
    lv4.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));} 
super.onActivityResult(requestCode, resultCode, data); 

if (requestCode == checklv1 && resultCode == RESULT_OK){ 
    ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
    lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));} 
super.onActivityResult(requestCode, resultCode, data); 

if (requestCode == checklv2 && resultCode == RESULT_OK){ 
    ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
    lv2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));} 
super.onActivityResult(requestCode, resultCode, data); 

if (requestCode == checklv3 && resultCode == RESULT_OK){ 
    ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
    lv3.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));} 
super.onActivityResult(requestCode, resultCode, data); 

} 

(修訂版)如果我用戶定時器,我有以下的代碼,但我在哪裏出了錯?哪裏放置super.onActivityResult...

 if (requestCode == check && resultCode == RESULT_OK){ 
     ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
     lv4.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results)); 
      new CountDownTimer(3000, 1000) { 

       public void onTick(long millisUntilFinished) { 
        // Do nothing for your logic. 
       } 
       public void onFinish() { 
        // Your logic here. 

       } 
      }.start();}} 
+0

我覺得這是不是做ü想要什麼正確的方式。爲什麼不使用'onActivityResult'開始下一個問題呢? –

+0

嗯,我嘗試了'onActivityResult',但仍然無法延遲工作@ρяσѕρєяK.是否能夠請幫助我調整上述代碼請提供 –

+0

使用onActivityResult顯示您的代碼 –

回答

2

代替處理程序,您可以使用Countdowntimer。

new CountDownTimer(3000, 1000) { 

    public void onTick(long millisUntilFinished) { 
    // Do nothing for your logic. 
    } 
    public void onFinish() { 
    // Your logic here. 
    } 
}.start(); 
+0

嗯聽起來很有趣,這是否發生在'onActivityResult'內,我將如何將它分配給'lv4'? –

+0

你可以在onFinish()中分配lv4,它在3秒後執行。你可以根據你的需求改變持續時間 –

+0

很好,所以我的代碼的一部分有'if(requestCode == check ...',然後以'super.onActivityResult ...'結尾......'這一切也都在'onFinish '部分? –

0

嘗試使用setClickable方法https://developer.android.com/reference/android/view/View.html#setClickable(boolean)

public void onClick(final View v){ 
    v.setClickable(false); 
    new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      Intent i1 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
      i1.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
      i1.putExtra(RecognizerIntent.EXTRA_PROMPT, "Is the information correct? (Yes/No)"); 
      v.setClickable(true); 
      startActivityForResult(i1, check); 
     } 
    }, 3000);