1、静态绑定下拉框数据
需要将数据写在xml中,然后设置下拉框的entries属性,则数据自动加载到下拉框中。具体如下:
在value文件夹中新建cityInfo.xml,xml中写入
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string-array name="cityArray">
- <item>北京</item>
- <item>江苏</item>
- <item>浙江</item>
- <item>上海</item>
- </string-array>
- </resources>
- <Spinner android:id="@+id/spinnerCityStatic"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:entries="@array/cityArray"/>
2、动态绑定下拉框数据
主要三个步骤,1.获得数据列表;2.填充数据适配器;3.设置下拉框的适配器;
- private Spinner spCity = null;
- private ArrayAdapter<CharSequence> adapterCity = null;
- private static String[] cityInfo={"北京","江苏","浙江","上海"};
- //初始化函数中代码如下
- this.spCity = (Spinner) super.findViewById(R.id.spinnerCity);
- //将数据cityInfo填充到适配器adapterCity中
- this.adapterCity = new ArrayAdapter<CharSequence>(this,
- android.R.layout.simple_spinner_dropdown_item, cityInfo);
- //设置下拉框的数据适配器adapterCity
- this.spCity.setAdapter(adapterCity);
3、选择时触发的事件
需要实现OnItemSelectedListener接口的onItemSelected方法,代码如下
- //下拉框选择事件
- private class OnItemSelectedListenerImpl implements OnItemSelectedListener {
- @Override
- public void onItemSelected(AdapterView<?> parent, View view,
- int position, long id) {
- String city = parent.getItemAtPosition(position).toString();
- Toast.makeText(MainActivity.this, "选择的城市是:" + city,
- Toast.LENGTH_LONG).show();
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- // TODO Auto-generated method stub
- }
- }
4、获得下拉框所选的值
- String cityStatic = spCityStatic.getSelectedItem().toString();
前台设计页面activity_main.xml如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <Spinner
- android:id="@+id/spinnerCityStatic"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:entries="@array/cityArray"/>
- <Spinner
- android:id="@+id/spinnerCity"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <Button
- android:id="@+id/btnShowCity"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="查看选择的结果"/>
- </LinearLayout>
- package com.example.spinner0803;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemSelectedListener;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.Spinner;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private Button btnShowCity = null;
- private Spinner spCityStatic = null;
- private Spinner spCity = null;
- private ArrayAdapter<CharSequence> adapterCity = null;
- private static String[] cityInfo={"北京","江苏","浙江","上海"};
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //按钮相关
- btnShowCity = (Button) super.findViewById(R.id.btnShowCity);
- btnShowCity.setOnClickListener(new OnClickListenerImpl());
- //静态实现的下拉框,数据写在cityInfo.xml文件中
- this.spCityStatic = (Spinner) super
- .findViewById(R.id.spinnerCityStatic);
- this.spCityStatic
- .setOnItemSelectedListener(new OnItemSelectedListenerImpl());
- //动态实现的下拉框,数据在程序中获得,实际项目可能来自数据库等
- this.spCity = (Spinner) super.findViewById(R.id.spinnerCity);
- this.adapterCity = new ArrayAdapter<CharSequence>(this,
- android.R.layout.simple_spinner_dropdown_item, cityInfo);
- this.spCity.setAdapter(adapterCity);
- this.spCity.setOnItemSelectedListener(new OnItemSelectedListenerImpl());
- }
- //按钮点击事件
- private class OnClickListenerImpl implements OnClickListener {
- @Override
- public void onClick(View v) {
- String cityStatic = spCityStatic.getSelectedItem().toString();
- String city = spCity.getSelectedItem().toString();
- String selectInfo = "第一个选择的城市是:" + city + ",第二个选择的城市是:"
- + cityStatic;
- Toast.makeText(MainActivity.this, selectInfo, Toast.LENGTH_LONG)
- .show();
- }
- }
- //下拉框选择事件
- private class OnItemSelectedListenerImpl implements OnItemSelectedListener {
- @Override
- public void onItemSelected(AdapterView<?> parent, View view,
- int position, long id) {
- String city = parent.getItemAtPosition(position).toString();
- Toast.makeText(MainActivity.this, "选择的城市是:" + city,
- Toast.LENGTH_LONG).show();
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- // TODO Auto-generated method stub
- }
- }
- }
原文地址:http://blog.csdn.net/pfe_Nova/article/details/38362193