帮助定位微调器

时间:2023-01-21 14:47:45

In my Android app, I have a sound that I want to play when a certain selection has been made from a spinner, but I want it to play the when the user actually makes the proper selection (or just after). My problem is that although the sound does play when they make the correct selection, as long as that selection stays chosen, it also plays every time the app starts up, when it should ONLY play at the time it's chosen. Here is the code I have now:

在我的Android应用程序中,当我从微调器中做出某个选择时,我有一个我想要播放的声音,但是我希望它在用户实际进行正确选择时(或之后)播放。我的问题是虽然声音确实在他们做出正确的选择时播放,但只要选择保持不变,它也会在每次应用启动时播放,此时应该只在选择时播放。这是我现在的代码:

    fitnessSpinner = (Spinner) findViewById(R.id.fitness_spinner);
    ArrayAdapter adapter4 = ArrayAdapter.createFromResource( 
        this, R.array.fitness_array, android.R.layout.simple_spinner_item); 
    adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    fitnessSpinner.setAdapter(adapter4);

    fitnessSpinner.setOnItemSelectedListener(new OnItemSelectedListener() 
    {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long i) {
            Log.d("test", "p: " + position + " " + i);
            if(position == 0) {
                //First Entry
                MediaPlayer mp =  MediaPlayer.create(mContext, R.raw.bowchica);
                mp.start();
            } if(position == 4) {
                MediaPlayer mp =  MediaPlayer.create(mContext, R.raw.debbie2);
                mp.start();
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {


        }

    });

How can I make the sound stop playing whenever the app is started? Should I wrap the whole fitnessSpinner.setOnItemSelectedListener... inside of some type of on change selection, if such a thing exist?

如何在应用程序启动时停止播放声音?我是否应该将整个fitnessSpinner.setOnItemSelectedListener包装在某种类型的更改选择中,如果存在这样的事情?

1 个解决方案

#1


0  

I could think of a workaround that you could use -

我可以想到你可以使用的解决方法 -

You could use a dummy entry in your array. Say if you have you want to put up a array of planets into a spinner then you could have array as -

您可以在数组中使用虚拟条目。如果你想要将一系列行星放入一个微调器,那么你可以将数组作为 -

List of Planets
Mars
Earth
Venus
..

When the application launches it will show an informative message as "List of planets" and in your callback you can have code like -

当应用程序启动时,它将显示一条信息性消息“行星列表”,在您的回调中,您可以使用以下代码:

if(0 != pos){
    //Play a sound
}

#1


0  

I could think of a workaround that you could use -

我可以想到你可以使用的解决方法 -

You could use a dummy entry in your array. Say if you have you want to put up a array of planets into a spinner then you could have array as -

您可以在数组中使用虚拟条目。如果你想要将一系列行星放入一个微调器,那么你可以将数组作为 -

List of Planets
Mars
Earth
Venus
..

When the application launches it will show an informative message as "List of planets" and in your callback you can have code like -

当应用程序启动时,它将显示一条信息性消息“行星列表”,在您的回调中,您可以使用以下代码:

if(0 != pos){
    //Play a sound
}