Spinner 下拉列表控件
创建一个activity_main.xml文件:
创建一个数组字符串:string.xml
MainActivity.java:- hello world
- yes i do!
- it\'s easy!
- I am javaer!
- Only a littly
package com.example.test.com;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.Window;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.Toast;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); //获取下拉列表对象 Spinner spinner = (Spinner) findViewById(R.id.spinner1); //获取字符串数组 final String[] stringArray = getResources().getStringArray(R.array.stringArray); //通过适配器 填充数据 ArrayAdapter效果图:adapter = new ArrayAdapter (this, android.R.layout.simple_spinner_item, stringArray); spinner.setAdapter(adapter); //设置一个选中的监听事件 spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) { int index = arg0.getSelectedItemPosition(); //选中的pos Toast.makeText(getBaseContext(), "SELECT: " + stringArray[index], Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView arg0) { } }); }}