本文主要是介绍Android组件之Spinner(下拉列表),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Android组件之Spinner(下拉列表)
分类: Android之路 2011-12-14 19:33 218人阅读 评论(0) 收藏 举报
Spinner是一个列表选择框,但其并不需要显示下拉列表,二十相当于一个菜单供用户选择,下面用一个例子介绍:
在样式文件中:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="请选择一项运动项目"
- />
- <Spinner
- android:id ="@+id/sportsSp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:prompt="@string/spinner_prompt"
- android:entries="@array/sports"
- />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string-array name="sports" >
- <item>足球</item>
- <item>篮球</item>
- <item>乒乓球</item>
- <item>网球</item>
- </string-array>
- </resources>
监听列表点击事件:
- package cn.class3g.activity;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemSelectedListener;
- import android.widget.Spinner;
- import android.widget.TextView;
- public class SpinnerDemo extends Activity implements OnItemSelectedListener{
- Spinner sportSp = null;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.spinner_layout);
- findViews();
- }
- private void findViews() {
- sportSp = (Spinner)this.findViewById(R.id.sportsSp);
- sportSp.setOnItemSelectedListener(this);
- sportSp.performClick();
- }
- //每选择一次均以日志输出形式打印
- public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- TextView tv = (TextView) arg1;
- Log.i("TAG", tv.getText().toString());
- }
- public void onNothingSelected(AdapterView<?> arg0) {
- }
- }
在模拟器中的效果与日志输出结果:
这篇关于Android组件之Spinner(下拉列表)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!