我是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
}
}
如果有人可以向我展示4个微调器的示例,那就太好了。提前致谢!
最佳答案
首先初始化您的四个微调器:
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;
}
}