XML布局文件:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.stackspinner.MainActivity" ><Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" /><Spinner android:id="@+id/spinner2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/spinner1" /></RelativeLayout>活动:public class MainActivity extends Activity implements OnItemSelectedListener {Spinner spinner1, spinner2;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner1 = (Spinner) findViewById(R.id.spinner1); spinner2 = (Spinner) findViewById(R.id.spinner2); ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this, R.array.array1, android.R.layout.simple_spinner_item); spinner1.setAdapter(adapter1); spinner1.setOnItemSelectedListener(this);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item);}@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) { if (spinner1.getSelectedItem().equals("mobile")) { Toast.makeText(getApplicationContext(), "Mobil dipilih", Toast.LENGTH_SHORT).show(); ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.mobile_array, android.R.layout.simple_spinner_item); spinner2.setAdapter(adapter2); } else { ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.motor_array, android.R.layout.simple_spinner_item); spinner2.setAdapter(adapter2); }}@Overridepublic void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub}}字符串:<resources><string name="app_name">Application Name</string><string name="hello_world">Hello world!</string><string name="action_settings">Settings</string><string-array name="array1"> <item>mobile</item> <item>motor</item></string-array><string-array name="mobile_array"> <item>Android</item> <item>blackberry</item> <item>apple</item></string-array><string-array name="motor_array"> <item>Audi</item> <item>BMW</item> <item>unicorn</item></string-array> I'm building a simple app for android which consists of spinners and edittext.question is I have 2 spinners with arrays in an adapterSpinner 1 ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this,R.array.kendaraan_arrays,android.R.layout.simple_spinner_item); spKendaraan.setAdapter(adapter1);what I want is arrays from spinner 2 is depends on what i select in spinner 1Spinner 2@Override public void onItemSelected (AdapterView < ? > adapterView, View view,int position, long id){ if (spKendaraan.getSelectedItem().equals("Mobil")) { Toast.makeText(getApplicationContext(), "Mobil dipilih", Toast.LENGTH_SHORT).show(); ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.pelanggaran_mobil, android.R.layout.simple_spinner_item); spPelanggaran.setAdapter(adapter2); } else { ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.pelanggaran_motor, android.R.layout.simple_spinner_item); spPelanggaran.setAdapter(adapter2); }}my code above doesn't work, am I making logic/method mistakes?tl;drif spinnerA == x{spinnerB = m}else {spinnerB = n}since i'm new in android, any example would be very appreciated. 解决方案 I don't know why your code is not working but following is a example code for you which works as you needed,I hope this will help.XML layout file:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.stackspinner.MainActivity" ><Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" /><Spinner android:id="@+id/spinner2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/spinner1" /></RelativeLayout>Activity:public class MainActivity extends Activity implements OnItemSelectedListener {Spinner spinner1, spinner2;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner1 = (Spinner) findViewById(R.id.spinner1); spinner2 = (Spinner) findViewById(R.id.spinner2); ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this, R.array.array1, android.R.layout.simple_spinner_item); spinner1.setAdapter(adapter1); spinner1.setOnItemSelectedListener(this);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item);}@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) { if (spinner1.getSelectedItem().equals("mobile")) { Toast.makeText(getApplicationContext(), "Mobil dipilih", Toast.LENGTH_SHORT).show(); ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.mobile_array, android.R.layout.simple_spinner_item); spinner2.setAdapter(adapter2); } else { ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.motor_array, android.R.layout.simple_spinner_item); spinner2.setAdapter(adapter2); }}@Overridepublic void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub}}Strings:<resources><string name="app_name">Application Name</string><string name="hello_world">Hello world!</string><string name="action_settings">Settings</string><string-array name="array1"> <item>mobile</item> <item>motor</item></string-array><string-array name="mobile_array"> <item>Android</item> <item>blackberry</item> <item>apple</item></string-array><string-array name="motor_array"> <item>Audi</item> <item>BMW</item> <item>unicorn</item></string-array> 这篇关于如何制作微调器取决于另一个微调器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-25 17:25