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...などのすべての項目が表示されます。スピナーの下の矢印をクリックすると、すべてのアイテムが1つずつ表示され、そのうちの1つを選択するとLOCALEとして使用されるようにしたいと思います。

だから私はこれをどうにかして修正する必要があります。

2つ目は、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

以下のスニペットが役に立ちます。

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 に答える
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 に答える