Litterally整天都花在这上面,目前仍然是一名java初学者。
我目前有spinner arri_spinner根据arri_spinners选择设置spinner s_spinner中的可用选项。但是我现在想要通过s_spinners选项填充thrid微调器prev_spinner。
不完全是app功能,但是例如arri_spinner可以选择颜色或单词。选择颜色,然后s_spinner有一个颜色列表,因此您可以从该列表中选择说蓝色。然后从arri_spinner中选择一种不同的颜色(在文本中)。
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left" >
<Spinner
android:id="@+id/arri_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="25dp"
android:entries="@array/larray"
android:prompt="@string/l_prompt" />
<Spinner
android:id="@+id/s_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/arri_spinner" />
<Spinner
android:id="@+id/prev_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/s_spinner"
android:entries="@array/prearray"
android:prompt="@string/pre_prompt" />
</RelativeLayout>
Java代码
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity {
public Spinner arri_spinner,arri_spinner2,s_spinner,s_spinner1,s_spinner2,s_spinner3,pre_spinner;
protected void onCreate(Bundle mbfSplash) {
super.onCreate(mbfSplash);
setContentView(R.layout.activity_main);
chooseL1();
chooseL();
chooseB();
chooseCe();
chooseCi();
}
public void chooseL1() {
arri_spinner = (Spinner) findViewById(R.id.arri_spinner);
ArrayAdapter<CharSequence> lineAdapter = ArrayAdapter.createFromResource(this, R.array.larray, android.R.layout.simple_spinner_item);
lineAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
arri_spinner.setAdapter(lineAdapter);
arri_spinner.setOnItemSelectedListener(new planOnClickListener());
}
private void chooseL(){
arri_spinner2 = (Spinner) findViewById(R.id.s_spinner);
List<String> planlist3 = new ArrayList<String>();
planlist3.add("Stat");
ArrayAdapter<String> planAdapter3 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, planlist3);
planAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
arri_spinner2.setAdapter(planAdapter3);
}
private void chooseB(){
s_spinner1 = (Spinner) findViewById(R.id.s_spinner);
List<String> planlist = new ArrayList<String>();
planlist.add("b temp1");
planlist.add("b temp2");
planlist.add("b temp3");
ArrayAdapter<String> planAdapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, planlist);
planAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s_spinner1.setAdapter(planAdapter1);
}
private void chooseCe(){
s_spinner2 = (Spinner) findViewById(R.id.s_spinner);
List<String> planlist1 = new ArrayList<String>();
planlist1.add("ce temp1");
planlist1.add("ce temp2");
ArrayAdapter<String> planAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, planlist1);
planAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s_spinner2.setAdapter(planAdapter2);
}
private void chooseCi(){
s_spinner3 = (Spinner) findViewById(R.id.s_spinner);
List<String> planlist2 = new ArrayList<String>();
planlist2.add("Ci temp1");
planlist2.add("Ci temp2");
ArrayAdapter<String> planAdapter3 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, planlist2);
planAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s_spinner2.setAdapter(planAdapter3);
}
public class planOnClickListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
parent.getItemAtPosition(pos);
if (pos==0){
chooseL();
}else if (pos==1){
chooseB();
}else if (pos==2){
chooseCe();
} else if (pos==3){
chooseCi();
}
}
public void onNothingSelected(AdapterView<?> parent) {
return;
}}}
字符串
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">test App</string>
<string name="action_settings">Settings</string>
<string name="l_prompt"></string>
<string name="pre_prompt"></string>
<string-array name="larray">
<item >Box1</item>
<item >B</item>
<item >Ce</item>
<item >Ci</item>
</string-array>
<string-array name="prearray">
<item ></item>
</string-array>
</resources>