Hy,我需要在旋转器上进行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);
如何使用执行此简单构造的onclicklistener:
sTarga=targhe[position];
答案 0 :(得分:7)
通过使用onitemselected侦听器,您可以在微调器中选择项目
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
//
}
});
答案 1 :(得分: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
}
});