Android开发--Spinner控件的使用
我们经常会在Windows开发的过程中看到一个下拉菜单控件,在Android中也有相应的控件,它的名字叫Spinner,本文介绍Spinner的用法。
首先,下面的这张截图是实现的实例:

在第一行,你会看到一行说明“This is just Test!”,这个类似于标题,它的定义方法是spinner.setPrompt("This is just Test!");
下面的数据是引用资源,使用的是string数组,让我们来看下数组的定义:
public class Spinner_Activiy extends Activity {private Spinner spinner=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_spinner__activiy);Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);// Create an ArrayAdapter using the string array and a default spinner layoutArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, android.R.layout.simple_spinner_item);// Specify the layout to use when the list of choices appearsadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// Apply the adapter to the spinnerspinner.setAdapter(adapter);spinner.setPrompt("This is just Test!");spinner.setOnItemSelectedListener(new SpinnerActivity());}public class SpinnerActivity implements OnItemSelectedListener { public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { // An item was selected. You can retrieve the selected item using // parent.getItemAtPosition(pos) Toast.makeText(Spinner_Activiy.this, "Your selection is "+parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show(); } public void onNothingSelected(AdapterView<?> parent) { // Another interface callback }}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_spinner__activiy, menu);return true;}}