注:本笔记英文原版就在你的电脑中:X:/Program Files/Android/android-sdk-windows/docs/resources/tutorials/views/hello-spinner.html(X即你安装SDK的盘符。)

本示例下载地址:http://download.csdn.net/source/3038894s

Spinner是一个类似于下拉列表的控件。

在本次学习笔记中,我将创建一个简单的Spinner用于显示一组行星列表。当选中其中一个时,将把先把的项目以吐司消息的形式显示出来。

1.在eclipse中新建一个名为HelloSpinner的Android项目;

2.打开res/layout/main.xml,并插入以为代码:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="10dip" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="@string/planet_prompt" /> <Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:prompt="@string/planet_prompt" /> </LinearLayout>

可以注意到TextView的android:text和Spinner的android:prompt都是指向一个string源。这是什么意思呢?就是这个string源的文字既可用作一个控件的标题,也可以在使用Spinner时,作为选择框弹出时的提示信息。

(TextView的android:text)

Spinner的android:prompt

3.在res/values/strings.xml中编辑如下:

<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">hello,</string> <string name="app_name">HelloSpinner</string> <string name="planet_prompt">Choose a planet</string> <string-array name="planets_array"> <item>Mercury</item> <item>Venus</item> <item>Earth</item> <item>Mars</item> <item>Jupiter</item> <item>Saturn</item> <item>Uranus</item> <item>Neptune</item> </string-array> </resources>

<string name="planet_prompt">Choose a planet</string>即是main.xml中TextView和Spinner的string源

<string-array>节点中的是用于显示在列表的中各项目。

4.现在打开HelloSpinner.java,并且插入以下代码在onCreate()中:

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner=(Spinner)findViewById(R.id.spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.planets_array, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); }

After the main.xml layout is set as the content view, the Spinner widget is captured from the layout with findViewById(int). The createFromResource() method then creates a new ArrayAdapter, which binds each item in the string array to the initial appearance for the Spinner (which is how each item will appear in the spinner when selected). The R.array.planets_array ID references the string-array defined above and the android.R.layout.simple_spinner_item ID references a layout for the standard spinner appearance, defined by the platform. Then setDropDownViewResource(int) is called to define the appearance for each item when the widget is opened (simple_spinner_dropdown_item is another standard layout defined by the platform). Finally, the ArrayAdapter is set to associate all of its items with the Spinner by calling setAdapter(T).

5.下面创建一个用于返回吐司消息的代码,和public void onCreate(Bundle savedInstanceState)并列:

public class MyOnItemSelectedListener implements OnItemSelectedListener { public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { Toast.makeText(parent.getContext(), "The planet is " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show(); } public void onNothingSelected(AdapterView<?> parent) { // Do nothing. }} } }

The AdapterView.OnItemSelectedListener requires the onItemSelected() and onNothingSelected() callback methods. The former is called when an item from the AdapterView is selected, in which case, a short Toast message displays the selected text; and the latter is called when a selection disappears from the AdapterView, which doesn't happen in this case, so it's ignored.

6.现在MyOnItemSelectedListener 需要被应用于Spinner了,在onCreate()方法的最后加入下列代码: spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

7.运行程序,下面是运行结果:

附:以下是我.java中全部代码,仅供参考:

package org.sample.SpinnerSample; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; public class SpinnerSample extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner=(Spinner)findViewById(R.id.spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.planets_array, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new MyOnItemSelectedListener()); } public class MyOnItemSelectedListener implements OnItemSelectedListener { public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { Toast.makeText(parent.getContext(), "The planet is " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show(); } public void onNothingSelected(AdapterView<?> parent) { // Do nothing. }} } } }

更多相关文章

  1. 使用ViewPager来实现Tab的效果
  2. Android绘制进阶之四:在位图上绘制文本并旋转
  3. android与其他应用的交互
  4. android AlertDialog布局 ——2
  5. Android侧拉框Demo
  6. Android(安卓)4.1/4.1.1 TextView.setText for Html 引发ArrayIn
  7. H5 调用android原生相机代码分析
  8. android 逆向工程-工具篇 jadx(九)
  9. 浅入浅出Android(007):看看你的手机上有哪些传感器

随机推荐

  1. Android(安卓)常用开发类库
  2. 如何实现一个图片加载框架
  3. flutter的AndroidX版本适配
  4. Android(安卓)编程下图片的内存优化
  5. Android(安卓)实现自定义闹钟
  6. Android录音aac格式
  7. Android(安卓)6.0 向用户申请权限,运行时
  8. android mvp快速开发框架介绍(自动生成an
  9. android studio 开发(二)问题
  10. 利用Android(安卓)UncaughtExceptionHand