我有8个微调器:
pl1sp = (Spinner) offersdialoglayout.findViewById(R.id.pl1offer);
pl2sp = (Spinner) offersdialoglayout.findViewById(R.id.pl2offer);
pl3sp = (Spinner) offersdialoglayout.findViewById(R.id.pl3offer);
pl4sp = (Spinner) offersdialoglayout.findViewById(R.id.pl4offer);
和
pl1scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl1scores);
pl2scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl2scores);
pl3scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl3scores);
pl4scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl4scores);
我需要设置2个监听器。一个用于前4个微调器,一个用于后4个。我不想重复这样的代码:
pl1sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
checkOffersSum(); // same method for first 4 spinners. for last 4 spinners is checkScoresSum()
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
});
pl2sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
checkOffersSum();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
});
等等.. 所以我只需要编写两次监听器,首先是前4个微调器,第二个是最后4个微调器。感谢
答案 0 :(得分:1)
您可以使用单个侦听器管理多个微调器。请检查this回答。它处理同样的问题。希望它有所帮助。
答案 1 :(得分:1)
请按照以下步骤操作:
在您的班级中实施OnItemSelectedListener
并覆盖未实施的方法onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3)
。
使用pl1sp.setOnItemSelectedListener(this)并为所有微调器执行此操作。
像这样使用overridenmethod onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3)
:
@Override
public void onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3) {
int id = view.getId();
if(id==R.id.pl1offer || id==R.id.pl2offer || id==R.id.pl3offer ||id==R.id.pl4offer){
// do this
}
else{
//do this
}
}
答案 2 :(得分:1)
为前4个微调器定义一个侦听器
Spinner[] offerspinners = {R.id.pl1offer, R.id.pl2offer, R.id.pl3offer, R.id.pl4offer};
for(Spinner offer : offerspinners){
offersdialoglayout.findViewById(offer).
setOnItemSelectedListener(offerListener);
}
AdapterView.OnItemSelectedListener offerListener =
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(parent.id){
case R.id.pl1offer:
// offer 1 related code
break;
case R.id.pl2offer:
// offer 2 related code
break;
}
// common code
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
}
比在此定义具有相同代码的第二个侦听器
Spinner[] scorespinners = {R.id.pl1scores, R.id.pl2scores, R.id.pl3scores, R.id.pl4scores};
for(Spinner score : scorespinners){
scoresdialoglayout.findViewById(score).
setOnItemSelectedListener(scoreListener);
}
AdapterView.OnItemSelectedListener scoreListener =
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(parent.id){
case R.id.pl1scores:
// score 1 related code
break;
case R.id.pl2scores:
// score 2 related code
break;
}
// common code
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
}
答案 3 :(得分:0)
你可以做的是创建一个包含所有微调器的数组,然后使用for循环将每个微调器连接到setItemSelected()方法,即
HT2010