生活随笔
收集整理的這篇文章主要介紹了
常用控件下拉列表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
零、學習目標
能說出下拉列表的基本用法
能利用下拉列表編寫簡單安卓應用
一、下拉列表概述
1、繼承關系圖
Spinner控件顯示單列數據,因此只能在程序里采用數組適配器來建立數據源與下拉列表之間的關聯。另外,有一種更簡單的綁定數據源的方式,直接在布局文件里設置下拉列表元素的entries屬性來綁定數組作為數據源。
2、常用屬性和方法
二、教學案例——選擇測試科目
(一)運行效果
(二)涉及知識點
線性布局(LinearLayout)
標簽(TextView)
數組適配器(ArrayAdapter)
吐司(Toast)
(三)實現步驟
1、創建安卓應用【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
下拉列表 - 選擇測試科目
測試科目:
安卓開發
Web開發
數據結構
網絡技術
Python編程
形勢與政策
5、啟動應用,查看效果
通過下拉列表的entries屬性綁定好了數據源,此時無須適配器也能看到下拉列表能展開列表項
6、主界面類 - MainActivity
聲明變量
通過資源標識符獲取控件實例
獲取測試科目數組
給下拉列表注冊監聽器
啟動應用,查看效果
8、修改主布局資源文件 - activity_main.xml
不給下拉列表設置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、啟動應用,查看效果
總結
以上是生活随笔為你收集整理的常用控件下拉列表的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。