首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >当方法结束时,如何等待?

当方法结束时,如何等待?
EN

Stack Overflow用户
提问于 2018-08-01 05:14:04
回答 1查看 77关注 0票数 0

我有下面的代码。请告诉我,如果我在TextView填充时调用getSpeechInput();(代码的底部),然后再次调用getSpeechInput(),该如何等待。我试过Thread.sleep,但它不起作用。谢谢。

package com.example.oco.test1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Locale;

import static java.lang.Thread.*;

public class MainActivity extends AppCompatActivity {

    private TextView txvResult;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txvResult = (TextView) findViewById(R.id.txvResult);
    }
    public void getSpeechInput() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent, 10);
        } else {
            Toast.makeText(this, "Your Device Dont Support Speech Input", Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 10:
                if (resultCode == RESULT_OK && data != null) {
                    ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    txvResult.setText(result.get(0));
                }
                break;
        }
    }
    public void getSTT(View view) {
       getSpeechInput();
       Thread.sleep(5000);
        getSpeechInput();
    }
}

EN

回答 1

Stack Overflow用户

发布于 2018-08-01 05:22:57

您正在启动一个活动,因此当它完成时,onActivityResult将被触发。在调用getSpeechInput之后,放入你想要做的任何事情。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51622159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档