Android 如何使TTS只需单击一次即可读取所有Listview项?

Android 如何使TTS只需单击一次即可读取所有Listview项?,android,android-listview,text-to-speech,Android,Android Listview,Text To Speech,我在android中有一个Listview,当您单击它读取的项目时,TTS可以读取它,但我的问题是如何让TTS通过只单击Listview中的任何项目一次来读取所有项目 这是密码 package learn2crack.listview; import java.util.ArrayList; import java.util.HashMap; import java.util.Locale; import org.json.JSONArray; import org.json.JSONEx

我在android中有一个Listview,当您单击它读取的项目时,TTS可以读取它,但我的问题是如何让TTS通过只单击Listview中的任何项目一次来读取所有项目

这是密码

 package learn2crack.listview;

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

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;



import learn2crack.listview.library.JSONParser;



public class MainActivity extends Activity implements TextToSpeech.OnInitListener {
ListView list;
TextView html;
Button Btngetdata;
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();

//URL to get JSON Array
private static String url = "http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=New%20York,NY&sensor=false"; //url is just a sample

//JSON Node Names 
private static final String TAG_OS = "routes";
private static final String TAG_HTM = "html_instructions";
private TextToSpeech myTTS;
private int MY_DATA_CHECK_CODE = 0;

JSONArray android = null;

 public void onInit(int initStatus) {

        // check for successful instantiation
        if (initStatus == TextToSpeech.SUCCESS) {
            if (myTTS.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE)
                myTTS.setLanguage(Locale.US);
        } else if (initStatus == TextToSpeech.ERROR) {
            Toast.makeText(this, "Sorry! Text To Speech failed...",
                    Toast.LENGTH_LONG).show();
        }
    }


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    oslist = new ArrayList<HashMap<String, String>>();



    Btngetdata = (Button)findViewById(R.id.getdata);
    Btngetdata.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
             new JSONParse().execute();


        }
    });


}


private class JSONParse extends AsyncTask<String, String, JSONObject> {
     private ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
         html = (TextView) findViewById(R.id.html_instruction);
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();



    }

    @Override
    protected JSONObject doInBackground(String... args) {

        JSONParser jParser = new JSONParser();

        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);
        return json;
    }
     @Override
     protected void onPostExecute(JSONObject json) {
         pDialog.dismiss();
         try {
                // Getting JSON Array from URL
                android = json.getJSONArray(TAG_OS);
                for (int x = 0; x < android.length(); x++) {
                    JSONArray legs = android.getJSONObject(x).getJSONArray("legs");
                    JSONObject distance = legs.getJSONObject(0).getJSONObject("distance");
                    JSONObject duration = legs.getJSONObject(0).getJSONObject("duration");
                    JSONArray steps = legs.getJSONObject(0).getJSONArray("steps");

                    for (int j = 0; j < steps.length(); j++) {
                            String html_instructions = steps.getJSONObject(j).getString("html_instructions");
                            String s = html_instructions.replaceAll("<(\"[^\"]*\"|'[^']*'|[^'\">])*>", " ");




                //for(int i = 0; i < android.length(); i++){
                //JSONObject c = android.getJSONObject(i);

                // Storing  JSON item in a Variable
                //String ver = c.getString(TAG_VER);
                //String name = c.getString(TAG_NAME);
                //String api = c.getString(TAG_API);
                //String htm = c.getString("html_instructions");



                // Adding value HashMap key => value


                HashMap<String, String> map = new HashMap<String, String>();


                map.put(TAG_HTM, s);

                oslist.add(map);
                list=(ListView)findViewById(R.id.list);





                final ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
                        R.layout.list_v,
                        new String[] {TAG_HTM}, new int [] {R.id.html_instruction});

                list.setAdapter(adapter);
                Intent checkTTSIntent = new Intent();
                checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
                startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {



                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        //Toast.makeText(MainActivity.this, "You Clicked at "+oslist.get(+position).get("html"), Toast.LENGTH_SHORT).show();
                        parent.getAdapter();
                        //adapter.getItem(position).toString();
                        String words = adapter.getItem(position).toString();
                       //  String words = html.getText().toString();
                        speakWords(words);

                    }
                    private void speakWords(String speech) {
                        //speak straight away
                        myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
                }

                });


                }
        }} catch (JSONException e) {
            e.printStackTrace();
        }



     }
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == MY_DATA_CHECK_CODE) {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            //the user has the necessary data - create the TTS
        myTTS = new TextToSpeech(this,(OnInitListener) this);
        }
        else {
                //no data - install it now
            Intent installTTSIntent = new Intent();
            installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installTTSIntent);
        }
    }
}
package learn2crack.listview;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Locale;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.speech.tts.TextToSpeech;
导入android.speech.tts.TextToSpeech.OnInitListener;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.Button;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.simpledapter;
导入android.widget.TextView;
导入android.widget.Toast;
导入learn2crack.listview.library.JSONParser;
公共类MainActivity扩展活动实现TextToSpeech.OnInitListener{
列表视图列表;
文本视图html;
按钮Btngetdata;
ArrayList oslist=新的ArrayList();
//获取JSON数组的URL
专用静态字符串url=”http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=New%20York,NY&sensor=false;//url只是一个示例
//JSON节点名称
私有静态最终字符串标记_OS=“routes”;
私有静态最终字符串标记\u HTM=“html\u指令”;
私密短信通话myTTS;
私有int MY_DATA_CHECK_CODE=0;
JSONArray-android=null;
public void onInit(int initStatus){
//检查实例化是否成功
if(initStatus==TextToSpeech.SUCCESS){
if(myTTS.islanguage可用(Locale.US)=TextToSpeech.LANG\u可用)
myTTS.setLanguage(Locale.US);
}else if(initStatus==TextToSpeech.ERROR){
Toast.makeText(此“抱歉!文本到语音转换失败…”,
Toast.LENGTH_LONG).show();
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
oslist=newarraylist();
Btngetdata=(按钮)findviewbyd(R.id.getdata);
Btngetdata.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
新建JSONParse().execute();
}
});
}
私有类JSONParse扩展了异步任务{
私人对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
html=(TextView)findViewById(R.id.html\u指令);
pDialog=新建进度对话框(MainActivity.this);
setMessage(“获取数据…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的JSONObject doInBackground(字符串…args){
JSONParser jParser=新的JSONParser();
//从URL获取JSON
JSONObject json=jParser.getJSONFromUrl(url);
返回json;
}
@凌驾
受保护的void onPostExecute(JSONObject json){
pDialog.disclose();
试一试{
//从URL获取JSON数组
android=json.getJSONArray(TAG_OS);
对于(intx=0;x,”);
//for(int i=0;ivalue
HashMap=newHashMap();
map.put(TAG_HTM,s);
添加(地图);
list=(ListView)findViewById(R.id.list);
最终列表适配器=新SimpleAdapter(MainActivity.this,oslist,
R.layout.list_v,
新字符串[]{TAG_HTM},新int[]{R.id.html_指令});
list.setAdapter(适配器);
Intent checkttsinent=新Intent();
checkttsinent.setAction(TextToSpeech.Engine.ACTION\u CHECK\u TTS\u数据);
startActivityForResult(检查输入、我的数据检查代码);
list.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//Toast.makeText(MainActivity.this),您在“+oslist.get(+position.get”(“html”)、Toast.LENGTH_SHORT.show()处单击过它;
parent.getAdapter();
//adapter.getItem(position.toString();
String words=adapter.getItem(position.toString();
//String words=html.getText().toString();
说单词(单词);
}
专用void speakWords(字符串语音){
//直接说
myTTS.speak(演讲、文本演讲)