gpt4 book ai didi

android - 多个微调器上的 OnItemSelectedListener 不工作

转载 作者:太空狗 更新时间:2023-10-29 12:50:17 24 4
gpt4 key购买 nike

我有一个包含多个微调器的 Activity (大约 8-9 个)。我使用的代码如下:

Spinner sp1, sp2, sp3, sp4, sp5;
sp1 = (Spinner) findViewById(R.id.spinner1);
sp2 = (Spinner) findViewById(R.id.spinner2);
sp3 = (Spinner) findViewById(R.id.spinner3);
sp4 = (Spinner) findViewById(R.id.spinner4);
sp5 = (Spinner) findViewById(R.id.spinner5);

sp1.setOnItemSelectedListener(this);
sp2.setOnItemSelectedListener(this);
sp3.setOnItemSelectedListener(this);
sp4.setOnItemSelectedListener(this);
sp5.setOnItemSelectedListener(this);

//Outside onCreate I'm using the following to handle the spinner Item Selected event.

@override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.spinner1:
Toast.makeText(getApplicationContext(), arg0.getItemAtPosition(arg2).toString(), Toast.LENGTH_LONG).show();
break;
case R.id.spinner2:
Toast.makeText(getApplicationContext(), arg0.getItemAtPosition(arg2).toString(), Toast.LENGTH_LONG).show();
break;
case R.id.spinner3:
Toast.makeText(getApplicationContext(), arg0.getItemAtPosition(arg2).toString(), Toast.LENGTH_LONG).show();
break;
case R.id.spinner4:
Toast.makeText(getApplicationContext(), arg0.getItemAtPosition(arg2).toString(), Toast.LENGTH_LONG).show();
break;
case R.id.spinner5:
Toast.makeText(getApplicationContext(), arg0.getItemAtPosition(arg2).toString(), Toast.LENGTH_LONG).show();
break;
}
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

但问题是我在任何微调器中选择项目时都没有得到任何 Toast。任何帮助,将不胜感激。提前致谢。

最佳答案

package com.meta;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.Toast;

import com.meta.R;

public class SpinnerActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner sp1, sp2, sp3, sp4, sp5;
sp1 = (Spinner) findViewById(R.id.spinner1);
sp2 = (Spinner) findViewById(R.id.spinner2);
sp3 = (Spinner) findViewById(R.id.spinner3);
sp4 = (Spinner) findViewById(R.id.spinner4);
sp5 = (Spinner) findViewById(R.id.spinner5);

sp1.setOnItemSelectedListener(myListener);
sp2.setOnItemSelectedListener(myListener);
sp3.setOnItemSelectedListener(myListener);
sp4.setOnItemSelectedListener(myListener);
sp5.setOnItemSelectedListener(myListener);



}

OnItemSelectedListener myListener=new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
switch (arg2) {
case 1:
Toast.makeText(SpinnerActivity.this,"Spinner 1", Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(SpinnerActivity.this, "Spinner 2", Toast.LENGTH_LONG).show();
break;
case 3:
Toast.makeText(SpinnerActivity.this, "Spinner 2", Toast.LENGTH_LONG).show();
break;
case 4:
Toast.makeText(SpinnerActivity.this,"Spinner 2", Toast.LENGTH_LONG).show();
break;
case 5:
Toast.makeText(SpinnerActivity.this, "Spinner 2", Toast.LENGTH_LONG).show();
break;
}

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
};



}

关于android - 多个微调器上的 OnItemSelectedListener 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12792714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com