生活随笔
收集整理的這篇文章主要介紹了
常用控件下拉列表
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
零、學(xué)習(xí)目標(biāo)
能說出下拉列表的基本用法
能利用下拉列表編寫簡單安卓應(yīng)用
一、下拉列表概述
1、繼承關(guān)系圖
Spinner控件顯示單列數(shù)據(jù),因此只能在程序里采用數(shù)組適配器來建立數(shù)據(jù)源與下拉列表之間的關(guān)聯(lián)。另外,有一種更簡單的綁定數(shù)據(jù)源的方式,直接在布局文件里設(shè)置下拉列表元素的entries屬性來綁定數(shù)組作為數(shù)據(jù)源。
2、常用屬性和方法
二、教學(xué)案例——選擇測試科目
(一)運(yùn)行效果
(二)涉及知識(shí)點(diǎn)
線性布局(LinearLayout)
標(biāo)簽(TextView)
數(shù)組適配器(ArrayAdapter)
吐司(Toast)
(三)實(shí)現(xiàn)步驟
1、創(chuàng)建安卓應(yīng)用【SelectSubject】
2、將背景圖片拷貝到drawable目錄
3、主布局資源文件activity_main.xml
<?xml version
="1.0" encoding
="utf-8"?><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
:background
="@drawable/background"
android
:padding
="15dp"
android
:orientation
="horizontal"
tools
:context
=".MainActivity"><TextView
android
:id
="@+id/tvTestSubject"
android
:layout_width
="wrap_content"
android
:layout_height
="wrap_content"
android
:textColor
="#0000ff"
android
:textSize
="25sp"
android
:text
="@string/test_subject"/><Spinner
android
:id
="@+id/spTestSubject"
android
:layout_width
="wrap_content"
android
:layout_height
="wrap_content"
android
:entries
="@array/subjects"/></LinearLayout
>
4、字符串資源文件strings.xml
下拉列表 - 選擇測試科目
測試科目:
安卓開發(fā)
Web開發(fā)
數(shù)據(jù)結(jié)構(gòu)
網(wǎng)絡(luò)技術(shù)
Python編程
形勢與政策
5、啟動(dòng)應(yīng)用,查看效果
通過下拉列表的entries屬性綁定好了數(shù)據(jù)源,此時(shí)無須適配器也能看到下拉列表能展開列表項(xiàng)
6、主界面類 - MainActivity
聲明變量
通過資源標(biāo)識(shí)符獲取控件實(shí)例
獲取測試科目數(shù)組
給下拉列表注冊(cè)監(jiān)聽器
啟動(dòng)應(yīng)用,查看效果
8、修改主布局資源文件 - activity_main.xml
不給下拉列表設(shè)置entries屬性
<?xml version
="1.0" encoding
="utf-8"?><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
:background
="@drawable/background"
android
:orientation
="horizontal"
android
:padding
="15dp"
tools
:context
=".MainActivity"><TextView
android
:id
="@+id/tvTestSubject"
android
:layout_width
="wrap_content"
android
:layout_height
="wrap_content"
android
:text
="@string/test_subject"
android
:textColor
="#0000ff"
android
:textSize
="16sp" /><Spinner
android
:id
="@+id/spTestSubject"
android
:layout_width
="wrap_content"
android
:layout_height
="wrap_content" /></LinearLayout
>
9、修改主界面類 - MainActivity
10、啟動(dòng)應(yīng)用,查看效果
總結(jié)
以上是生活随笔為你收集整理的常用控件下拉列表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。