Android Spinner控件使用方法
来源:互联网 发布:安卓手机安全卫士源码 编辑:程序博客网 时间:2024/04/28 07:51
Spinner是适配器控件的一种,提供一种下拉列表形式的控件,运行后效果如下:
使用方法比较简单,直接上代码
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:text="@string/ys" android:id="@+id/TextView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="28sp"/> <Spinner android:id="@+id/Spinner01" android:layout_width="fill_parent" android:layout_height="wrap_content"/></LinearLayout>
string.xml
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">Spinner</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="ys">您的爱好</string> <string name="lq">篮球</string> <string name="zq">足球</string> <string name="pq">排球</string></resources>
color.xml
<?xml version="1.0" encoding="utf-8"?><resources><color name="red">#fd8d8d</color><color name="green">#9cfda3</color><color name="blue">#8d9dfd</color><color name="white">#FFFFFF</color><color name="black">#000000</color></resources>
MainActivity.java
package com.mzhq.spinner;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.Spinner;import android.widget.TextView;import android.view.ViewGroup;import android.widget.AdapterView.OnItemSelectedListener;public class MainActivity extends Activity { final static int WRAP_CONETNT = -2;// 表示WRAP_CONTENT的常量 // 所有资源的图片(足球、篮球、排球) id的数组 int[] drawableIds = { R.drawable.football, R.drawable.basketball, R.drawable.volleyball }; // 所有资源字符串 (足球、篮球、排球) id的数组 int[] msgIds = { R.string.zq, R.string.lq, R.string.pq }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Spinner sp = (Spinner) findViewById(R.id.Spinner01); BaseAdapter ba = new BaseAdapter() { public int getCount() { // 一共三个选项 return 3; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } public View getView(int position, View convertView, ViewGroup parent) { // 动态生成每个下拉项对应的View,每个下拉项View由LinearLayout // 中包含一个ImageView及一个TextView构成 // 初始化LinearLayout LinearLayout ll = new LinearLayout(MainActivity.this); ll.setOrientation(LinearLayout.HORIZONTAL); // 初始化ImageView ImageView ii = new ImageView(MainActivity.this); ii.setImageDrawable((getResources().getDrawable(drawableIds[position]))); ll.addView(ii); // 初始化TextView TextView tv = new TextView(MainActivity.this); tv.setText(" " + getResources().getText(msgIds[position])); tv.setTextColor(getResources().getColor(R.color.red)); tv.setTextSize(24); ll.addView(tv); return ll; } }; // 为Spinner设置内容适配器 sp.setAdapter(ba); sp.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // 获取主界面TextView TextView tv = (TextView) findViewById(R.id.TextView01); // 获取当前选中选项对应的LinearLayout LinearLayout ll = (LinearLayout) view; // 获取其中的TextView TextView tvn = (TextView) ll.getChildAt(1); // 用StringBuilder动态生成信息 StringBuilder sb = new StringBuilder(); sb.append(getResources().getText(R.string.ys)); sb.append(":"); sb.append(tvn.getText()); // 信息设置进住界面 tv.setText(sb.toString()); } public void onNothingSelected(AdapterView<?> parent) { } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }}
源码下载地址:http://download.csdn.net/detail/m370809968/6367685
- Android Spinner控件使用方法
- android 常用控件之 (Spinner )使用方法
- android spinner的使用方法
- Android 控件之Spinner
- Android常用控件-spinner
- android控件14 Spinner
- Android Spinner控件
- Android 控件之Spinner
- android控件之spinner
- Android Spinner控件
- android控件--Spinner
- Android控件之Spinner
- Android常用控件-spinner
- Android基本控件---Spinner
- android学习---Spinner控件
- Android控件之Spinner
- android Spinner控件详解
- android Spinner控件详解
- 数组中的逆序对
- cassandra配置
- C++学习道路
- swing学习英文档
- JAVA 获取农历
- Android Spinner控件使用方法
- OpenGL ES2学习笔记(3)-- 使用Shader
- 解决eclipse不自动提示(Alt+/)
- 这些话,是乔布斯给世间留下的真正伟大礼物
- requestWindowFeature()的应用
- Servlet和JSP的生命周期
- Python模块学习——email(实例)
- C#几个经常用到的字符串的截取、替换
- C# ASP.NET MVC HtmlHelper用法大全