简体   繁体   English

Android-从微调器中选择项目后,如何使微调器显示项目?

[英]Android -How do i make the Spinner to show the item after selecting the Items from the Spinner?

This is my code: 这是我的代码:

    package com.testotspeech;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
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;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class AndroidTestToSpeechActivity extends Activity implements
        TextToSpeech.OnInitListener, OnItemSelectedListener {
    /** 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;
    private ArrayAdapter<String> dataAdapter;
    private TextView textview;
    private Iterator itr;
    private String[] t = {"Please Select An Item"};

    @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);
        spinner.setOnItemSelectedListener(this);


        dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,t);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        spinner.setAdapter(dataAdapter);
        textview = (TextView)findViewById(R.id.textView1);






        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.ENGLISH);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                btnSpeak.setEnabled(true);
                speakOut();

            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }

    }

    private void speakOut() {

        String text = txtText.getText().toString();
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {

        if (position == 0)
        {
        }
        else
        {

        }
    }

    public void onNothingSelected(AdapterView<?> parent) {
        textview.setText("");
    }


}

EDITED: 编辑:

What i want to do is when i click on thw spinner it will collapse/open down and i will have for each item and box of it self and it will be in row for example i clicked on the spinner i will see now under it: 我想做的是,当我单击微调器时,它会折叠/打开,我将为它的每个项目和盒子装上它自己,它将排成一排,例如,我单击微调器,我现在会看到它下面:

Hello 你好

Bye 再见

Daniel 丹尼尔

and now if i click on Hello it will put the Hello in the textView1 if i click on the Bye it will put it also in textView1 and so on. 现在,如果我单击“ Hello”,它将把Hello放入textView1中;如果我单击“再见”,它将把它也放入textView1中,依此类推。 But the graphics the designing of the spinner i want it to be that when i click on it it will collapse down and show me the items in a row so i can click and select each item in a single click. 但是图形设计的微调器是我想要的,当我单击它时它会折叠并连续显示给我看项目,这样我就可以单击并单击选择每个项目。

Now what i did is just adding the spinner a text "Please Select An Item" I uploaded image of how i wanted it to be like for example: 现在,我只是向微调器添加文本“请选择一个项目”,例如,我上传了我想要的图像:

微调框示例

That is the behavior of the spinner, there is always a selected item. 那就是微调器的行为, 总是有一个选定的项目。

You can create a flag and use that in an override of the onItemSelected , put a counter in there and use that to ignore the first time in. 您可以创建一个标志,并在onItemSelected的替代中使用该标志,在onItemSelected放置一个计数器,并使用该标志忽略第一次。

Like this: 像这样:

private int spinnerSelectCount = 0;

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    if(spinnerSelectCount == 0) {
        // Do nothing... initial item on spinner display is the selected item
    } else {
        // your code to process spinner selection here
    }
    }
});

EDIT 编辑

You need to put your string as the first item in your array, not a separate string. 您需要将字符串作为数组中的第一项而不是单独的字符串。

itemsList = new ArrayList<String>();  
itemsList.add("Please Select An Item");
itemsList.add(Arrays.toString(Locale.getAvailableLocales())); 

Then call the spinner using the array: 然后使用数组调用微调器:

    dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,itemsList);        
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);        
    spinner.setAdapter(dataAdapter);  

And since you're doing that you should change the filter to be based on position, not count. 而且由于这样做,您应该将过滤器更改为基于位置,而不是计数。

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    if(pos == 0) {
        // Do nothing... initial item on spinner display is the selected item
    } else {
        // your code to process spinner selection here
        textview.setText(itemsList.get(pos));
    }
    }
});

EDIT 2 编辑2

Are you looking for multi-selection? 您在寻找多重选择吗? That's not possible with a standard spinner, it is set up to select one item only. 使用标准微调器是不可能的,它只能选择一个项目。 If you are really looking to select multiple items you need to use a list or create a custom spinner. 如果您确实要选择多个项目,则需要使用列表或创建自定义微调器。

Fortunately, it looks like someone has already done that. 幸运的是,看起来已经有人这样做了。 You can find the code in this SO Answer 您可以在此SO Answer中找到代码

EDIT 3 编辑3

The MultiSpinner does not need a new project. MultiSpinner不需要新项目。 You merely create a new class and use that to populate your spinner. 您只需创建一个新类,然后使用它来填充微调框。

1) Create a new class called MultiSpinner 1)创建一个名为MultiSpinner的新类
2) Copy the code from link (leave out the package name as you'll want to use your own) 2)从链接中复制代码(省略软件包名称,因为您想使用自己的软件包)
3) In your xml call out the spinner like so (replacing com.yourpackage.name with your actual package name): 3)在您的xml中像这样调出微调器(用实际的包名替换com.yourpackage.name ):

<com.yourpackage.name.MultiSpinner android:id="@+id/multi_spinner" /> 

4) Call the spinner as shown in the link: 4)调用微调器,如链接中所示:

MultiSpinner multiSpinner = (MultiSpinner) findViewById(R.id.multi_spinner); 
multiSpinner.setItems(items, getString(R.string.for_all), 
                this); 

An easy solution would be to set the first item of the spinner to a text like "Please select a value..". 一个简单的解决方案是将微调器的第一项设置为“请选择一个值..”之类的文本。 Then in the code.. 然后在代码中..

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    if(pos == 0) {
    // do nothing..
    } else {
    // your code..
    }
}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM