下拉列表Spinner的使用
来源:互联网 发布:高清混合矩阵价格 编辑:程序博客网 时间:2024/05/16 11:46
1.在res/values/strings.xml文件中加入字符串资源
<string name="ys">您的爱好</string> <string name="lq">篮 球</string> <string name="zq">足 球</string> <string name="pq">排 球</string>
2.准备颜色资源在res/values目录下新建colors.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>
3.设置布局。在main.xml文件中写如下代码
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:text="@string/ys" android:id="@+id/TextView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="28dip"/> <Spinner android:id="@+id/Spinner01" android:layout_width="fill_parent" android:layout_height="wrap_content"/></LinearLayout>
4.源文件这样写
import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;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.widget.AdapterView.OnItemSelectedListener;public class Spinner extends Activity {final static int WRAP_CONTENT=-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 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner sp=(Spinner)this.findViewById(R.id.Spinner01);//初始化Spinner BaseAdapter ba=new BaseAdapter(){//为Spinner准备内容适配器@Overridepublic int getCount() {return 3;}//总共三个选项@Overridepublic Object getItem(int arg0) { return null; }@Overridepublic long getItemId(int arg0) { return 0; }@Overridepublic View getView(int arg0, View arg1, ViewGroup arg2) {// 动态生成每个下拉项对应的View,每个下拉项View由LinearLayout//中包含一个ImageView及一个TextView构成//初始化LinearLayoutLinearLayout ll=new LinearLayout(Sample_5_9.this);ll.setOrientation(LinearLayout.HORIZONTAL);//设置朝向//初始化ImageViewImageView ii=new ImageView(Sample_5_9.this);ii.setImageDrawable(getResources().getDrawable(drawableIds[arg0]));//设置图片ll.addView(ii);//添加到LinearLayout中//初始化TextViewTextView tv=new TextView(Sample_5_9.this);tv.setText(" "+getResources().getText(msgIds[arg0]));//设置内容tv.setTextSize(24);//设置字体大小tv.setTextColor(R.color.black);//设置字体颜色ll.addView(tv);//添加到LinearLayout中return ll;} }; sp.setAdapter(ba);//为Spinner设置内容适配器 sp.setOnItemSelectedListener(//设置选项选中的监听器 new OnItemSelectedListener(){@Overridepublic void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {//重写选项被选中事件的处理方法TextView tv=(TextView)findViewById(R.id.TextView01);//获取主界面TextViewLinearLayout ll=(LinearLayout)arg1;//获取当前选中选项对应的LinearLayoutTextView tvn=(TextView)ll.getChildAt(1);//获取其中的TextView StringBuilder sb=new StringBuilder();//用StringBuilder动态生成信息sb.append(getResources().getText(R.string.ys));sb.append(":");sb.append(tvn.getText());tv.setText(sb.toString());//信息设置进主界面TextView}@Overridepublic void onNothingSelected(AdapterView<?> arg0) { } } ); }}
0 0
- Spinner下拉列表的使用
- spinner下拉列表的使用
- spinner下拉列表的使用
- 下拉列表Spinner的使用
- Spinner-下拉列表的使用
- 【下拉列表】spinner的使用
- Android-下拉列表框Spinner的使用
- 下拉列表框Spinner的使用
- 使用PopupWindow实现Spinner的下拉列表
- 使用Spinner实现下拉列表
- 使用Spinner实现下拉列表
- 使用Spinner实现下拉列表
- 使用Spinner实现下拉列表
- 使用Spinner实现下拉列表
- 使用Spinner实现下拉列表
- 使用Spinner实现下拉列表
- Spinner类控件的使用(下拉列表的使用)
- Spinner 下拉列表的用法
- Ubuntu14.04搭建Oracle instantClient 11.2.0.4并配置cx_Oracle5.1.2
- 将代码托管到GitHub
- 杭电 oj 1002 A + B Problem II 记录
- 笔记91--WindowManager.LayoutParams应用之实现悬浮窗口
- 图最少着色问题
- 下拉列表Spinner的使用
- 项目迷途
- TQ2440开发板 tftp下载并烧写内核
- spring
- 与猪(pig)共舞
- ICON的设计很重要
- Ubuntu系统下搭建lnmp环境
- Android应用开发中如何使用RSA加密算法对数据进行校验
- Linux下卸载oracle_RAC