Android常用组件.docx
- 文档编号:8473882
- 上传时间:2023-01-31
- 格式:DOCX
- 页数:24
- 大小:153.44KB
Android常用组件.docx
《Android常用组件.docx》由会员分享,可在线阅读,更多相关《Android常用组件.docx(24页珍藏版)》请在冰豆网上搜索。
Android常用组件
1、课程名称:
Android常用组件
2、知识点
2.1、上次课程的主要知识点
2.2、本次预计讲解的知识点
3、具体内容
3.1、常用表单组件(掌握)
在Android中,也可以通过组件实现HTML表单中的各种功能,但这里不包含隐藏域的组件,因为没有必要。
常用的表单组件中,比较复杂的是选择类的组件,这里可以使用以下的组件实现:
单选,多选,下拉列表,日期选择,时间选择的功能。
3.1.1、单选,多选
需要在XML中声明这两种组件。
android=" xmlns: tools=" android: layout_width="match_parent" android: layout_height="match_parent" android: background="@drawable/login_bg" android: orientation="vertical" android: paddingBottom="5dp" android: paddingLeft="5dp" android: paddingRight="5dp" android: paddingTop="5dp"> android: layout_width="match_parent" android: layout_height="wrap_content" android: text="性别: " android: textColor="#000000"/> android: layout_width="match_parent" android: layout_height="wrap_content" android: orientation="vertical"> android: text="男" android: textColor="#000000"/> android: text="女" android: textColor="#000000"/> 单选应该还支持默认选中功能。 android: text="男" android: checked="true" android: textColor="#000000"/> 但要注意,这样写有bug,如果写上为true,则永远无法再进行切换,一直为true 因此建议在程序中进行默认选中的设置。 android: id="@+id/radio" android: layout_width="match_parent" android: layout_height="wrap_content" android: orientation="vertical"> publicclassMainActivityextendsActivity{ privateRadioGroupradio; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); //设置所使用的布局界面 setContentView(R.layout.activity_main); radio=(RadioGroup)findViewById(R.id.radio); //可以通过check()方法来设置选中某一个单选,但要传递该单选按钮的id值. //如果为每个单选按钮设置id,比较麻烦,因此这里通过按钮的position下标,直接取得按钮,再通过getId()取得id值来设置. radio.check(radio.getChildAt(0).getId()); } } 如果选项不是写死的形式,而是通过传递的集合数据动态生成的,可以通过程序来进行选项的建立。 publicclassMainActivityextendsActivity{ privateRadioGroupradio; privateString[]allValues={"男","女","未知"}; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); //设置所使用的布局界面 setContentView(R.layout.activity_main); radio=(RadioGroup)findViewById(R.id.radio); //可以通过check()方法来设置选中某一个单选,但要传递该单选按钮的id值. //如果为每个单选按钮设置id,比较麻烦,因此这里通过按钮的position下标,直接取得按钮,再通过getId()取得id值来设置. //radio.check(radio.getChildAt(0).getId()); //动态为单选按钮组加入选项 //循环数组或集合,建立单选按钮 for(inti=0;i RadioButtonbutton=newRadioButton(this); button.setText(allValues[i]); button.setTextColor(Color.BLACK); radio.addView(button); } } } 多选框使用方法与单选基本相同,只不过换成CheckBox 这里没有CheckBoxGroup,可以直接使用LinearLayout替代之前的RadioGroup。 android: id="@+id/checkbox" android: layout_width="match_parent" android: layout_height="wrap_content" android: orientation="vertical"> android: layout_width="match_parent" android: layout_height="wrap_content" android: checked="true" android: text="巴厘岛" android: textColor="#000000"/> android: layout_width="match_parent" android: layout_height="wrap_content" android: checked="true" android: text="马尔代夫" android: textColor="#000000"/> android: layout_width="match_parent" android: layout_height="wrap_content" android: text="三亚" android: textColor="#000000"/> android: layout_width="match_parent" android: layout_height="wrap_content" android: text="香格里拉" android: textColor="#000000"/> android: layout_width="match_parent" android: layout_height="wrap_content" android: checked="true" android: text="马来西亚" android: textColor="#000000"/> 3.1.2、Spinner下拉列表 下拉列表中的数据不能直接加入,可以通过XML来进行固定的配置,或使用Adpater来动态加入。 写死的数据,可以打开values.xml,来加入一段配置。 \ android: id="@+id/spinner" android: entries="@array/area_array" android: layout_width="match_parent" android: layout_height="wrap_content"/> 这里的@array的数据就是在strngs.xml中声明的StringArray数组 如果想动态生成数据,也要通过程序完成,需要建立一个ArrayAdapter的类,来转换传入的List集合或String[]数据。 //取得Spinner组件 spinner=(Spinner)findViewById(R.id.spinner); //根据数据,建立适配器 adapter=newArrayAdapter android.R.layout.simple_spinner_item,allAreaValues); //设置选项弹出后的显示样式 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); 如果不想使用系统自带的显示样式,也可以自己在res/layout中声明一个自定义的TextView的样式。 android=" xmlns: tools=" android: layout_width="wrap_content" android: textColor="#ff0000" android: textSize="15sp" android: layout_height="wrap_content"> //根据数据,建立适配器 adapter=newArrayAdapter R.layout.my_spinner_item,allAreaValues); 3.1.3、日期时间选择器、滚动面板 日期选择器的设置 android: layout_width="match_parent" android: layout_height="wrap_content" android: text="选择生日: " android: textColor="#000000"/> android: layout_width="match_parent" android: layout_height="wrap_content"/> 但这时,内容已经超出了屏幕范围,因此需要加入一个滚动面版,来实现屏幕滚动的功能。 滚动面版中要求只能有一个子节点。 因此这里要将滚动面版ScrollView加入到最外面,作为根节点来使用 android=" xmlns: tools=" android: layout_width="match_parent" android: layout_height="match_parent"> android: layout_width="match_parent" android: layout_height="wrap_content" android: background="@drawable/login_bg" android: orientation="vertical" android: paddingBottom="5dp" android: paddingLeft="5dp" android: paddingRight="5dp" android: paddingTop="5dp"> 同时,里面的LinearLayout的高度,必须设置成为wrap_content 如果想使用水平滚动支持,可以加入水平滚动面版 还可以取消滚动条的显示 android=" xmlns: tools=" android: layout_width="match_parent" android: layout_height="match_parent" android: scrollbars="none"> 时间选择也可以通过TimePicker来实现。 android: layout_width="match_parent" android: layout_height="wrap_content"/> 3.2、ListView(重点) 只要想进行数据列表,必须要用到ListView列表组件,该组件可以将一组数据,循环列表显示到界面上,并自带滚动功能。 android: id="@+id/list" android: layout_width="match_parent" android: layout_height="match_parent"> 使用时,也要通过Adapter来动态加入数据,根据表格的格式,需要选择不同的Adapter来完成。 如果表格一行中只有一段字符串,没有进行格式处理,可以使用ArrayAdapter。 例如: 如果表格中每一行的数据格式比较复杂,但是里面没有图片,可以使用SimpleAdapter来实现,例如: 如果每行中还有图片显示,则要自定义Adapter来完成,例如: 先使用ArrayAdapter来完成基本的单行文字显示功能 3.2.1、ArrayAdapter 使用方法类似Spinner。 publicclassMainActivityextendsActivity{ privateList privateArrayAdapter privateListViewlist; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); //设置所使用的布局界面 setContentView(R.layout.activity_main); //取得组件 list=(ListView)findViewById(R.id.list); //初始化要添加到表格中的数据 for(inti=0;i<50;i++){ allValues.add("添加的数据"+i); } //建立Adapter adapter=newArrayAdapter android.R.layout.simple_list_item_1,allValues); list.setAdapter(adapter); } } 3.2.2、SimpleAdapter 如果没有图片显示,可以使用SimpleAdapter处理复杂格式。 使用这个Adapter之前,需要先编写一个layout下的xml布局文件,描述每一行的布局格式。 android=" xmlns: tools=" android: layout_width="match_parent" android: layout_height="wrap_content" android: background="#ffffff" android: orientation="horizontal" android: paddingBottom="5dp" android: paddingLeft="5dp" android: paddingRight="5dp" android: paddingTop="5dp"> android: layout_width="0dp" android: layout_height="wrap_content" android: layout_weight="5" android: orientation="vertical"> android: id="@+id/line_title" android: layout_width="match_parent" android: layout_height="wrap_content" android: textColor="#66cdaa" android: textSize="14sp"/> android: id="@+id/line_time" android: layout_width="match_parent" android: layout_height="wrap_content" android: textColor="#cccccc" android: textSize="10sp"/> android: id="@+id/line_area" android: layout_width="match_parent" android: layout_height="wrap_content" android: textColor="#cccccc" android: textSize="10sp"/> android: id="@+id/line_price" android: layout_width="0dp" android: layout_height="wrap_content" android: layout_weight="1" android: gravity="center_horizontal" android: textColor="#FF6600" android: textSize="12sp"/> 在程序中,建立SimpleAdapter,读取这个布局,并将数据与布局中的TextView组件绑定。 publicclassMainActivityextendsActivity{ //由于每一行数据所对应的数据量不再是一条,而是多条,而且要根据名称进行区分,所以List中每一条数据是一个Map集合. privateList privateSimpleAdapteradapter; privateListViewlist; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); //设置所使用的布局界面 setContentView(R.layout.activity_main); //取得组件 list=(ListView)findViewById(R.id.list); //初始化要添加到表格中的数据 for(inti=0;i<50;i++){ Map map.put("title","商品标题信息"+i); map.put("time","9成新"+i+"小时前发布"); map.put("area","江苏南通"); map.put("price",(int)(Math.random()*1000)+"元"); allValues.add(map); } //建立Adapter adapter=newSimpleAdapter(this,allValues, R.layout.simple_adapter_list_line,newString[]{"title", "time","area","price"},newint[]{R.id.line_title, R.id.line_time,R.id.line_area,R.id.line_price}); list.setAdapter(adapter); } } 通过属性,可以控制分隔线的颜色 android: id="@+id/list" android: divider="#dddddd" android: cacheColorHint="#00000000" android: layout_width="match_parent" android: layout_height="
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Android 常用 组件