Android实战简易教程<一>(Spinner控件详解)

下拉列表框是一种常见的图形组件,与其他选择组件相比,可以有效的节省屏幕空间,在Android中可以使用android.widget.Spinner类来实现。

下拉列表框中的列表项有以下两种配置方式。

方式一、通过资源文件配置,例如定义一个values\city_data.xml的文件,在定义数据内容时需要使用<string-array>元素指定,定义内容如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string-array name="city_labels">  
  4.         <item>北京 </item>  
  5.         <item>上海 </item>  
  6.         <item>广州 </item>  
  7.         <item>深圳 </item>  
  8.     </string-array>  
  9. </resources>  


方式二、通过android.widget.ArrayAdapter类读取资源文件或者指定具体设置的数据。

方式一

1.定义main.xml文件

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/text"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="请选择您喜欢的城市:" />  
  12.   
  13.     <Spinner  
  14.         android:id="@+id/spinner"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:entries="@array/city_labels" ><!-- 载入数据 -->  
  18.     </Spinner>  
  19.   
  20. </LinearLayout>  


其实这是你就可以运行模拟器了:

这是可以看到数据已经加入到Spinner里面去了,我们发现这时的控件只是徒有其表,没有什么作用,要想实现监听怎么做呢?

我们改动一下main.xml文件:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="请选择您喜欢的城市:" />  
  11.   
  12.     <Spinner  
  13.         android:id="@+id/spinner"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:entries="@array/city_labels" > <!-- 载入数据 -->  
  17.     </Spinner>  
  18.   
  19.     <TextView  
  20.         android:id="@+id/text"  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="wrap_content" />  
  23.   
  24. </LinearLayout>  

然后编辑MainActivity文件:

[java]  view plain copy
  1. package org.yayun.demo;  
  2.   
  3. import org.yayun.demo.R;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.AdapterView;  
  9. import android.widget.TextView;  
  10. import android.widget.AdapterView.OnItemSelectedListener;  
  11. import android.widget.Spinner;  
  12.   
  13. public class MainActivity extends Activity {  
  14.     private Spinner spinner;  
  15.     private TextView textView;  
  16.   
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState); // 生命周期方法  
  19.         super.setContentView(R.layout.main); // 设置要使用的布局管理器  
  20.         spinner = (Spinner) findViewById(R.id.spinner);  
  21.         textView = (TextView) findViewById(R.id.text);  
  22.         spinner.setOnItemSelectedListener(new OnItemSelectedListener() {  
  23.   
  24.             public void onItemSelected(AdapterView<?> parent, View view,  
  25.                     int position, long id) {  
  26.                 String[] cities = getResources().getStringArray(  
  27.                         R.array.city_labels);//获取列表数据  
  28.                 textView.setText("您喜欢的城市是:" + cities[position]);//显示  
  29.   
  30.             }  
  31.   
  32.             public void onNothingSelected(AdapterView<?> parent) {  
  33.                 // TODO Auto-generated method stub  
  34.   
  35.             }  
  36.         });  
  37.   
  38.     }  
  39. }  

运行如下:



方式二

修改main.xml代码:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="请选择您喜欢的城市:" />  
  11.   
  12.     <Spinner  
  13.         android:id="@+id/spinner"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:entries="@array/city_labels" > <!-- 载入数据 -->  
  17.     </Spinner>  
  18.   
  19.     <TextView  
  20.         android:id="@+id/text"  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="wrap_content" />  
  23.     <Spinner  
  24.         android:id="@+id/spinnerCountry"  
  25.         android:layout_width="fill_parent"  
  26.         android:layout_height="wrap_content" > <!-- 动态载入数据 -->  
  27.     </Spinner>  
  28.   
  29. </LinearLayout>  

修改MainActivity程序:

[java]  view plain copy
  1. package org.yayun.demo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.widget.AdapterView;  
  10. import android.widget.AdapterView.OnItemSelectedListener;  
  11. import android.widget.ArrayAdapter;  
  12. import android.widget.Spinner;  
  13. import android.widget.TextView;  
  14. import android.widget.Toast;  
  15.   
  16. public class MainActivity extends Activity {  
  17.     private Spinner spinner, spinnerCountry;  
  18.     private TextView textView;  
  19.     private List<CharSequence> data = null;  
  20.     private ArrayAdapter<CharSequence> adapter;  
  21.   
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState); // 生命周期方法  
  24.         super.setContentView(R.layout.main); // 设置要使用的布局管理器  
  25.         spinner = (Spinner) findViewById(R.id.spinner);  
  26.         textView = (TextView) findViewById(R.id.text);  
  27.         spinnerCountry = (Spinner) findViewById(R.id.spinnerCountry);  
  28.   
  29.         spinnerCountry.setPrompt("选择国籍:");// 在列表中显示  
  30.         data = new ArrayList<CharSequence>();  
  31.         data.add("中國");  
  32.         data.add("美國");  
  33.         data.add("日本");  
  34.         adapter = new ArrayAdapter<CharSequence>(this,  
  35.                 android.R.layout.simple_spinner_dropdown_item, this.data);//定义下拉列表  
  36.         spinnerCountry.setAdapter(adapter);  
  37.         spinner.setOnItemSelectedListener(new OnItemSelectedListener() {  
  38.   
  39.             public void onItemSelected(AdapterView<?> parent, View view,  
  40.                     int position, long id) {  
  41.                 String[] cities = getResources().getStringArray(  
  42.                         R.array.city_labels);// 获取列表数据  
  43.                 textView.setText("您喜欢的城市是:" + cities[position]);// 显示  
  44.   
  45.             }  
  46.   
  47.             public void onNothingSelected(AdapterView<?> parent) {  
  48.                 // TODO Auto-generated method stub  
  49.   
  50.             }  
  51.         });  
  52.         spinnerCountry.setOnItemSelectedListener(new OnItemSelectedListener() {  
  53.   
  54.             public void onItemSelected(AdapterView<?> parent, View view,  
  55.                     int position, long id) {  
  56.                 String[] countries = data.toArray(new String[data.size()]);// 获取列表数据  
  57.                 Toast.makeText(MainActivity.this"您的国籍是:"+countries[position], Toast.LENGTH_SHORT).show();  
  58.                   
  59.                   
  60.             }  
  61.   
  62.             public void onNothingSelected(AdapterView<?> parent) {  
  63.                 // TODO Auto-generated method stub  
  64.                   
  65.             }  
  66.         });  
  67.   
  68.     }  
  69. }  


运行实例如下:

总结:

1.定义数据内容时需要使用<string-array>元素指定;

2.android:entries="@array/city_labels"载入文本资源;

3.String[] cities = getResources().getStringArray(R.array.city_labels);//获取资源数据的方法

4.String 和 CharSequence 关系
String 继承于CharSequence,也就是说String也是CharSequence类型。
CharSequence是一个接口,它只包括length(), charAt(int index), subSequence(int start, int end)这几个API接口。除了String实现了CharSequence之外,StringBuffer和StringBuilder也实现了CharSequence接口。
需要说明的是,CharSequence就是字符序列,String, StringBuilder和StringBuffer本质上都是通过字符数组实现的!

5.提示信息的设置:spinnerCountry.setPrompt("选择国籍:");// 在列表中显示

6.此外可以用adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//来设置显示风格

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值