android - 在多次点击后延迟一个 Intent ?

标签 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();}}

最佳答案

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

new CountDownTimer(3000, 1000) {

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

关于android - 在多次点击后延迟一个 Intent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38365742/

相关文章:

android - DataBinding 在 Android Studio 2.3 中不起作用,无法转换为 android.databinding.tool.expr.StaticIdentifierExpr

java - 警报对话框异常 "Unable to add windows -- token null is not for an application",我应该提供什么上下文?

android - 在自定义 ListView 上实现搜索

Android:使用图标作为后退按钮而不重新加载以前的 Activity

android - 禁用其他应用程序注册的 Intent

Android Camera Intent 与 Android L 崩溃

android - 如何更新应用程序?

Android NDK clang 编译器错误在 Windows 上显示 'No such file or directory'

android - 如何管理和引用特定的依赖库版本?

java - NotificationListenerService sendBroadcast 不起作用?