在活动启动时获取选择的微调项目
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView> parent,View view,int position,long id) {
Toast.makeText(parent.getContext(),"The country is " +
position,Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView> parent) {
return;
}
});
最佳答案
您必须使用标志来维护该状态.当您的Activity启动时,Spinner已经将其第一个项目选中,因此在启动Activity时会调用它的onItemSelected.
你可以通过这个管理它,取两个int变量.
int first_spinner = 0,first_spinner_counter = 0;
现在初始化微调器时设置first_spinner = 1;然后添加监听器
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView> parent,long id) {
if (first_spinner_counter < first_spinner) {
first_spinner_counter++;
}
else
{
Toast.makeText(parent.getContext(),Toast.LENGTH_LONG).show();
}
}
public void onNothingSelected(AdapterView> parent) {
return;
}
});