我是Android的新手,想知道如何在一个页面上拥有4个微调器,但是依赖于彼此的价值。我试图这样做,以便当您选择第一个的值时,例如第二个微调器的值可能会改变,但我需要第三个微调器依赖于第二个微调器的值,与第四个微调器相同取决于第三个微调器的值(因为我有这么多的可能性)。我正确设置了布局我只是无法让我的主类中的代码正常工作。
我在网上发现了类似的内容,描述了如何使用2个微调器,但我需要它有4个。
package com.bar.example.androidspinnerexample;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
public class SpinnerEx4Activity extends Activity implements
OnItemSelectedListener{
Spinner s1,s2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_ex4);
s1 = (Spinner)findViewById(R.id.spinner1);
s2 = (Spinner)findViewById(R.id.spinner2);
s1.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String sp1= String.valueOf(s1.getSelectedItem());
Toast.makeText(this, sp1, Toast.LENGTH_SHORT).show();
if(sp1.contentEquals("Income")) {
List<String> list = new ArrayList<String>();
list.add("Salary");
list.add("Sales");
list.add("Others");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
s2.setAdapter(dataAdapter);
}
if(sp1.contentEquals("Expense")) {
List<String> list = new ArrayList<String>();
list.add("Conveyance");
list.add("Breakfast");
list.add("Purchase");
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
s2.setAdapter(dataAdapter2);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
&#13;
如果有人能给我看一个4个微调器的例子,那就太棒了。提前谢谢!
答案 0 :(得分:2)
首先初始化你的四个微调器:
s1 = (Spinner)findViewById(R.id.spinner1);
s2 = (Spinner)findViewById(R.id.spinner2);
s3 = (Spinner)findViewById(R.id.spinner3);
s4 = (Spinner)findViewById(R.id.spinner4);
然后设置监听器:
s1.setOnItemSelectedListener(this);
s2.setOnItemSelectedListener(this);
s3.setOnItemSelectedListener(this);
s4.setOnItemSelectedListener(this);
并实施您的onItemSelected
方法,如下所示:
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int spinnerId = view.getId();
switch(spinnerId) {
case R.id.spinner1:
// do spinner 1 stuff
// spinner 1 has changed update spinner 2 here
break;
case R.id.spinner2:
// do spinner 2 stuff
// spinner 2 has changed update spinner 3 here
break;
case R.id.spinner3:
// do spinner 3 stuff
// spinner 3 has changed update spinner 4 here
break;
case R.id.spinner4:
// do spinner 4 stuff
break;
}
}