2011-08-16 59 views
0
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. 
} 

}如何返回微調器選擇?

+0

我需要一種方法來返回在微調器上選擇哪個項目。 :[ – Souichiro

+0

spinner.getSelectedItem(),spinner.getSelectedItemId() – Rasel

+0

嘗試'soundSelection = Integer.parseInt(spinner.getSelectedItem()。toString());' –

回答

0

這裏是解決如何從微調

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

0
`onItemSelected` 

正在返回你的位置,並使用這個位置,你需要提取價值。當你正在使用的資源,所以你應該在你的本地變量,並從他們可以獲取價值得到它的價值....

+0

是否有可能爲您給我看一個例子?如果沒有太多要問。 :[ – Souichiro

+0

在變量中獲取數據,如下所示:String [] var = getResources()。getStringArray(R.array.soundEntries);和String spinSelection = var [position] .. –

+0

我開始懷疑是否有可能爲此使用Switch。 – Souichiro