1
package com.testotspeech;

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

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class AndroidTestToSpeechActivity extends Activity implements
        TextToSpeech.OnInitListener {
    /** Called when the activity is first created. */

    private TextToSpeech tts;
    private Button btnSpeak;
    private EditText txtText;
    private ArrayList<String> itemsList;
    private Spinner spinner;
    private String contry_name;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.i("----------",Arrays.toString(Locale.getAvailableLocales()));
        itemsList = new ArrayList<String>();
        itemsList.add(Arrays.toString(Locale.getAvailableLocales()));
        spinner = (Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,itemsList);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);

        contry_name=String.valueOf(spinner.getSelectedItem());
        System.out.println(contry_name);


        tts = new TextToSpeech(this, this);
        btnSpeak = (Button) findViewById(R.id.btnSpeak);
        txtText = (EditText) findViewById(R.id.txtText);

        // button on click event
        btnSpeak.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                speakOut();
            }

        });
    }

    @Override
    public void onDestroy() {
        // Don't forget to shutdown tts!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    public void onInit(int status) {

        if (status == TextToSpeech.SUCCESS) {

            int result = tts.setLanguage(Locale.);

我有一些问题。首先在微调器中,在运行应用程序时进行任何选择之前,我会看到所有类似这样的项目:[en, en_AU, en_BE,en_BW...等等。我希望当我单击微调器的向下箭头时,我会一一看到所有项目,当我选择其中一个项目时,它将用作 LOCALE

所以我需要以某种方式解决这个问题。

第二件事是在 int result = tts.setLanguage(Locale.); 中选择项目时如何使用 contry_name 字符串 ?

现在是: int result = tts.setLanguage(Locale.ENGLISH); 但相反,我希望它成为微调器中的选定项目。

4

3 回答 3

2
      package com.validation;

        import android.app.Activity;
        import android.os.Bundle;
        import android.view.KeyEvent;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.ArrayAdapter;
        import android.widget.Button;
       import android.widget.Spinner;



        public class Validation_edittextActivity extends Activity {
            /** Called when the activity is first created. */



            Spinner country_spinner;

     String[] COUNTRIES = new String[] {
                    "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
                    "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
                    "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
                    "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium"};


            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

    country_spinner=(Spinner)findViewById(R.id.spinner1);

            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,COUNTRIES);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            country_spinner.setAdapter(dataAdapter);


String contry_name=String.valueOf(country_spinner.getSelectedItem());
System.out.println(contry_name);
    }
于 2012-06-06T07:14:13.753 回答
0

尝试这个

Spinner spinner = (Spinner) findViewById(R.id.spinner1);
itemsList = new ArrayList<String>();
itemsList.add(Arrays.toString(Locale.getAvailableLocales()));

ArrayAdapter<String> adapter = new ArrayAdapter(AndroidTestToSpeechActivity.this ,    itemsList,android.R.layout.simple_spinner_item);       

spinner.setAdapter(adapter);
于 2012-06-06T06:00:26.797 回答
0

下面的片段会帮助你。

package com.sample;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Spinner;
    import android.widget.TextView;

    public class SpinnerDemo Activity implements
            AdapterView.OnItemSelectedListener {
        TextView selection;
        String[] items = { "1", "2", "3", "4", "5", "6","7", "8", "9", "10" };

        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.main);
            selection = (TextView) findViewById(R.id.selection);

            Spinner spin = (Spinner) findViewById(R.id.spinner);
            spin.setOnItemSelectedListener(this);

            ArrayAdapter aa = new ArrayAdapter(
                    this,
                    android.R.layout.simple_spinner_item, 
                    items);

            aa.setDropDownViewResource(
               android.R.layout.simple_spinner_dropdown_item);
            spin.setAdapter(aa);
        }

        public void onItemSelected(AdapterView<?> parent, View v, int position,
                long id) {
            selection.setText(items[position]);
        }

        public void onNothingSelected(AdapterView<?> parent) {
            selection.setText("");
        }
    }//class
于 2012-06-06T05:58:30.920 回答