下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】Android中如何使用Spinner组件
今天就跟大家聊聊有关Android中如何使用Spinner组件,可能很多人都不太了解,为了让大家更加了解,在下给大家总结了以下内容,希望大家根据这篇文章可以有所收获。AndroidSpinner组件Spinner:下拉组件使用事项:布局在XML中实现,具体的数据在JAVA代码中实现;所用知识点:数组适配器:ArrayAdapter
用于关系M层和C层;事件:OnItemSelectedListener;案列:查看十二星座效果图:xml:代码如下:<LinearLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
>
<Spinner
android:id="@+id/spinner"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="@+id/showInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center"
android:text="
"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>java代码如下:
package
com.example.spinnertest;
import
java.util.ArrayList;
import
android.app.Activity;
import
android.content.res.Resources;
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.TextView;
public
class
MainActivity
extends
Activity
{
private
Spinner
spinner;
private
TextView
tx;
private
ArrayList<String>
list
=
null;
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*
获取TextView
实体对象
,
用于显示星座的详细信息
*/
tx
=
(TextView)
findViewById(R.id.showInfo);
/*
下拉组件测试
Spinner
对象首先获取
*/
spinner
=
(Spinner)
findViewById(R.id.spinner);
/*
准备数据源
M
,
用集合进行保存
*/
list
=
new
ArrayList<String>();
list.add("Aries");
list.add("Taurus");
list.add("Gemini");
list.add("Cancer");
list.add("Leo");
list.add("Virgo");
list.add("Libra");
list.add("Scorpio");
list.add("Sagittarius");
list.add("Capricorn");
list.add("Aquarius");
list.add("Pisces");
/*
实现M
层
与C
层的关系
,绑定数据
*/
/*
参数1:上下文对象;
参数2:系统资源布局方式
;
参数3:数据对象
*/
ArrayAdapter<String>
adapter
=
new
ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item,
list);
/*
对V
层和C
层进行关系的绑定;
*/
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new
OnItemSelectedListener()
{
/*
对所有的选择项进行监控
*//*
参3:当前选择项的ID
参4:被选择项在组中的位置,逻辑上与ID
相等,但代表的意义不一样
*/
public
void
onItemSelected(AdapterView<?>
arg0,
View
arg1,
int
id,
long
position)
{
//
Toast.makeText(MainActivity.this,
"你选择的是第:"+id+"值为:"+list.get(id),
1000).show();
/*
设置tx对象的值
*/
String
temp
=
getConstellation(id);
tx.setText("\t"+temp);
}
@Override
public
void
onNothingSelected(AdapterView<?>
arg0)
{
//
TODO
Auto-generated
method
stub
}
});
}
/*
显示星座的信息
*/
protected
String
getConstellation(int
id){
Resources
rs
=
getResources();
String
temp
=
"";
switch(id){
case
0:
temp
=
rs.getString(R.string.Aries);
break;
case
1:
temp
=
rs.getString(R.string.Taurus);
break;
case
2:
temp
=
rs.getString(R.string.Gemini);
break;
case
3:
temp
=
rs.getString(R.string.Cancer);
break;
case
4:
temp
=
rs.getString(R.string.Leo);
break;
case
5:
temp
=
rs.getString(R.string.Virgo);
break;
case
6:
temp
=
rs.getString(R.string.Libra);
break;
case
7:
temp
=
rs.getString(R.string.Scorpio);
break;
case
8:
temp
=
rs.getString(R.string.Sagittarius);
break;
case
9:
temp
=
rs.getString(R.string.Capricorn);
break;
case
10:
temp
=
rs.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 房地产企业财务审计师面试题及答案
- 门户运营面试题集
- 2025年多功能智能社区平台建设项目可行性研究报告
- 2025年农业数据系统建设项目可行性研究报告
- 2025年手机应用开发项目可行性研究报告
- 2025年智能制造与自动化生产项目可行性研究报告
- 2026年山西艺术职业学院单招职业技能测试题库及完整答案详解1套
- 2026年南开大学滨海学院单招职业倾向性考试题库附答案详解
- 2026年南京特殊教育师范学院单招职业倾向性测试题库及答案详解一套
- 2026年德州科技职业学院单招职业技能考试题库参考答案详解
- 2025年度物流行业市场调研:产业规模、政策支持及数字化趋势报告
- 2025年及未来5年市场数据中国拖拉机制造市场竞争态势及投资战略规划研究报告
- 广东省广州市越秀区2024-2025学年八年级上学期期末考试英语试题
- 地震波速反演方法-洞察及研究
- 百年未有之大变局课件
- 2025年时事政治考试100题及答案
- 应急救援电源
- 电力行业电力工程设计师岗位招聘考试试卷及答案
- 2025年北京市建筑施工作业人员安全生产知识教育培训考核试卷E卷及答案
- 2024年云南省第一人民医院招聘考试真题
- 2025急性高甘油三酯血症胰腺炎康复期多学科管理共识解读
评论
0/150
提交评论