如何使用微调

我对android非常陌生。 我想在我的申请中使用2个纺纱机,一个显示国家列表,当select任何国家时,另一个纺纱机应显示该国的城市列表。 当select城市时,会执行一些操作。 PLZ帮我一些示例代码。 感谢预期

Solutions Collecting From Web of "如何使用微调"

这里是我们可以用来添加选项到spinner2和微调1的东西。

public class Activity extends Activity implements View.OnClickListener { private Spinner spinner0, spinner1, spinner2, spinner3; private Button submit, cancel; private String country[], state[], city[], area[]; Australia aus = new Australia(); Object object; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); spinner0 = (Spinner)findViewById(R.id.spinnerCountry); spinner1 = (Spinner)findViewById(R.id.spinnerQ1); spinner2 = (Spinner)findViewById(R.id.spinnerQ2); spinner3 = (Spinner)findViewById(R.id.spinnerQ3); submit = (Button)findViewById(R.id.btnSubmit); cancel = (Button)findViewById(R.id.btnCancel); submit.setOnClickListener(this); cancel.setOnClickListener(this); country = new String[] {"Select Country", "Australia", "USA", "UK", "New Zealand", "EU", "Europe", "China", "Hong Kong", "India", "Malaysia", "Canada", "International", "Asia", "Africa"}; ArrayAdapter<String> adapter0 = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, country); adapter0.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner0.setAdapter(adapter0); Log.i("AAA","spinner0"); spinner0.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View view1, int pos, long id) { Log.i("AAA","OnItemSelected"); int loc; loc = pos; switch (loc) { case 1: state = aus.getState(); object = aus; Log.i("AAA","ArrayAdapter1"); ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, state); adapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner1.setAdapter(adapter1); Log.i("AAA","spinner1"); break; default: Log.i("AAA","default 0"); break; } } @Override public void onNothingSelected(AdapterView<?> arg1) { Log.i("AAA","Nothing S0"); } }); spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View view1, int pos, long id) { Log.i("AAA","OnItemSelected S1"); int loc = pos; switch(loc) { case 1: Log.i("AAA","Australia"); if(object.equals(aus)) { city = aus.getType(loc); } else { break; } ArrayAdapter<String> adapter = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, city); adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner2.setAdapter(adapter); Log.i("AAA","spinner2"); break; default: Log.i("AAA", "default 1"); break; } } @Override public void onNothingSelected(AdapterView<?> arg0) { Log.i("AAA","Nothing S1"); } }); spinner2.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long id) { int loc = pos; switch (loc) { case 1: if(object.equals(aus)) { area = aus.getTitle(loc); } ArrayAdapter<String> adapter = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, area); adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner3.setAdapter(adapter); Log.i("","spinner3"); break; default: break; } } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); }// on-create @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnSubmit: break; case R.id.btnCancel: finish(); break; default: break; } } } 

如果你觉得这个有用,那就把它放弃投票,这样别人就可以很容易地find一个很好的答案。

对于每个国家,您必须为其创build一个class级,只需添加州,城市和地区。 这样它就不会成为单个网页上的网格。

玩的开心。

问候,

HAPS。

下面是一个示例代码,它描述了在select微调项目时执行的微调控件和操作

 import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.AdapterView.OnItemSelectedListener; public class MainActivity extends Activity { Spinner spin; String spin_val; String[] gender = { "Male", "Female" };//array of strings used to populate the spinner @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//setting layout spin = (Spinner) findViewById(R.id.spinner_id);//fetching view's id //Register a callback to be invoked when an item in this AdapterView has been selected spin.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) { // TODO Auto-generated method stub spin_val = gender[position];//saving the value selected } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); //setting array adaptors to spinners //ArrayAdapter is a BaseAdapter that is backed by an array of arbitrary objects ArrayAdapter<String> spin_adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, gender); // setting adapteers to spinners spin.setAdapter(spin_adapter); } } 

要向微调器添加一个值列表,你需要在你的Activity中指定一个SpinnerAdapter,它扩展了Adapter类。一个微调适配器允许定义两个不同的视图:一个显示微调器本身的数据,一个显示下拉列表中的数据在按下微调器时。您也可以下载&参考完整的spinner_demo示例项目,以便更好地理解。