我是android新手。我试图构建一个有2个微调器的应用程序... 在深入了解我的申请之前,我几乎没有疑问。
请简要回答我的问题......
setOnItemSelectedListener(this)
吗?spinner.getSelectedItemPosition();
一样?等待你的宝贵回复。参考资料也很明显
答案 0 :(得分:13)
是的,您可以拥有多个可以由单个侦听器管理的微调器,或者您可以为每个微调器分别配置一个侦听器。
对于Single Listener,你可以做这样的事情,
设置监听器
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
然后您可以使用onItemSelected
来执行与微调器相关的操作。
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position,long arg3)
{
int id = parent.getId();
switch (id)
{
case R.id.spinner1:
// your stuff here
break;
case R.id.spinner2:
// your stuff here
break;
}
答案 1 :(得分:1)
检查一下:
Spinner spinner0,spinner1,spinner2; TextView textView0,textView1,textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView0 = (TextView) findViewById(R.id.text0);
spinner0 = (Spinner) findViewById(R.id.spinner0);
ArrayAdapter<String> adapter0 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, text0);
adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner0.setAdapter(adapter0);
spinner0.setOnItemSelectedListener(onItemSelectedListener0);
textView1 = (TextView) findViewById(R.id.text1);
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, text1);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setSelection(0,false); //make sure only one listener is
//通过将另一个侦听器设置为false,在onCreate方法中处于活动状态 spinner1.setOnItemSelectedListener(onItemSelectedListener1);
textView2 = (TextView) findViewById(R.id.text2);
spinner2 = (Spinner) findViewById(R.id.spinner2);
MySpinnerAdapter adapter2 = new MySpinnerAdapter(MainActivity.this, android.R.layout.simple_spinner_item, obj2);
//adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setSelection(0,false);
spinner2.setOnItemSelectedListener(onItemSelectedListener2);
}
OnItemSelectedListener onItemSelectedListener0 = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do your stuff spinner0
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
OnItemSelectedListener onItemSelectedListener1 = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do your stuff spinner1
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
OnItemSelectedListener onItemSelectedListener2 = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do your stuff spinner2
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
}
答案 2 :(得分:0)
如果两个微调器的引用不同(如果你使用2个微调器),则为它们提供不同的ID,你可以唯一地识别它们,是的,你可以单独设置它们的点击监听器。
spinner.setOnItemSelectedListener(new OnItemSelectedListener() { ... } );
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { ... } );
答案 3 :(得分:0)
spinner1.getSelectedItemPosition()
,spinner2.getSelectedItemPosition()