2012-05-30 58 views
1

Hy,我需要一個微調onclick。 這是我的微調:onclick在微調?

Spinner spinner = (Spinner) findViewById(R.id.spnTarghe); 
final String targhe[]=risposta.split("/"); 
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
     this, 
     android.R.layout.simple_spinner_item, 
     targhe 
     ); 
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

spinner.setAdapter(adapter); 

我如何使用做,做這個簡單的istruction的onclicklistener:

 sTarga=targhe[position]; 

回答

7

使用onitemselected聽衆可以選擇spinner中的物品

 spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> arg0, View arg1, 
       int arg2, long arg3) { 
      // TODO Auto-generated method stub 
      String mselection=spinner.getSelectedItem().toString();  
      Toast.makeText(getApplicationContext(), "selected "+ mselection, 30).show();    
      /**** do your code*****/ 
     } 
     @Override 
     public void onNothingSelected(AdapterView<?> arg0) { 
      // TODO Auto-generated method stub 
     // 
     } 
    }); 
2

使用onItemSelected

spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
    @Override 
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { 
     // your code here 
      sTarga=targhe[position]; 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parentView) { 
     // your code here 
    } 

});