如何返回微调器选择?

时间:2011-08-16 04:12:10

标签: java android

package com.RaffDev.RaffApp;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;


public class RaffAppActivity extends Activity implements View.OnClickListener, AdapterView.OnItemSelectedListener{

/*Regular Variables*/
String copyright = "Raff App \n\nVersion: Alpha 0.0.1\n\n(c) Copyright Raff App contributors and others 2011. All rights reserverd";
MediaPlayer mp;
public int soundSelection = 0;


/*Called at the beginning of the application*/
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.soundEntries, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter); 



}

/*Button Functions*/
public void onClick(View v){}

public void infoClick(View v){

    Toast.makeText(this, copyright, Toast.LENGTH_LONG).show();

}

public void pressPlay(View v){
    switch(soundSelection){
    case 1: 
        if(mp != null && mp.isPlaying()) mp.reset();
        mp = MediaPlayer.create(getApplicationContext(), R.raw.pleaseprettyface);
        mp.start();
        break;
    case 2: 
        if(mp != null && mp.isPlaying()) mp.reset();
        mp = MediaPlayer.create(getApplicationContext(),R.raw.sheeesh);
        mp.start();
        break;
    case 3: 
        if(mp != null && mp.isPlaying()) mp.reset();
        mp = MediaPlayer.create(getApplicationContext(),R.raw.heaintdodat);
        mp.start();
        break;
    case 4: 
        if(mp != null && mp.isPlaying()) mp.reset();
        mp = MediaPlayer.create(getApplicationContext(),R.raw.thedousky);
        mp.start();
        break;
    case 5: 
        if(mp != null && mp.isPlaying()) mp.reset();
        mp = MediaPlayer.create(getApplicationContext(), R.raw.nmouth);
        mp.start();
        break;


    }
}


/*Spinner Functions*/
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    soundSelection = (Integer) parent.getItemAtPosition(pos);
}

public void onNothingSelected(AdapterView<?> parent) {
      // Do nothing.
}

}

2 个答案:

答案 0 :(得分:0)

`onItemSelected`

返回你的位置并使用这个位置你需要提取值。当您使用资源时,您应该在本地变量中获取其值,并从中获取值....

答案 1 :(得分:0)

以下是解决方案如何从Spinner获取所选项目

public class YourItemSelectedListener implements OnItemSelectedListener {

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    String selected = parent.getItemAtPosition(pos).toString();
}

public void onNothingSelected(AdapterView parent) {
    // Do nothing.
}
}

了解更多信息请参阅 enter link description here