多次onClick后延迟意图?

时间:2016-07-14 04:53:30

标签: android android-studio android-intent android-edittext onclicklistener

我正在创建一个应用程序,一个接一个地连续询问用户一系列问题。然而。我问的最后一个问题是"信息是否正确?"

我需要此提示在其他一系列连续问题之后出现至少3000ms,以便用户首先查看记录的信息并查看是否全部正确。但是目前,无论使用处理程序,最后的提示会在第二个问题之后立即出现。有没有办法在最终提示出现之前至少延迟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();}}

2 个答案:

答案 0 :(得分:2)

而不是处理程序,您可以使用Countdowntimer。

new CountDownTimer(3000, 1000) {

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

答案 1 :(得分: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);
相关问题