Android 下拉列表

一、案例操作

1、创建安卓应用【SelectSubject】

image.png

2、将背景图片拷贝到drawable目录

image.png

3、主布局资源文件activity_main.[xml]

image.png

<?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

image.png

<resources>
    <string name="app_name">下拉列表 - 选择测试科目</string>
    <string name="test_subject">测试科目:</string>
    
    <string-array name="subjects">
        <item>安卓开发</item>
        <item>Web开发</item>
        <item>数据结构</item>
        <item>网络技术</item>
        <item>Python编程</item>
        <item>形势与政策</item>
    </string-array>
</resources>

5、启动应用,查看效果

  • 通过下拉列表的entries属性绑定好了数据源,此时无须适配器也能看到下拉列表能展开列表项
    VIDEO1.gif

6、主界面类 - MainActivity

  • 声明变量

image.png

  • 通过资源标识符获取控件实例

image.png

  • 获取测试科目数组

image.png

  • 给下拉列表注册监听器

image.png

7、启动应用,查看效果

VIDEO1.gif

上面, 我们没有采用适配器来绑定数据源,直接利用下拉列表的entries属性来绑定字符串资源文件里定义的字符串数组。其实,我们也可以采用数组适配器来绑定数据源。

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9g8oToEB-1671341562870)(https://cdn.nlark.com/yuque/0/2022/png/33577488/1671341413665-4a0af98b-1171-4ec3-ba87-0db911f90d7c.png#averageHue=%23f7f6f5&clientId=u5c1aadd4-10c4-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=818&id=u45a2f587&name=image.png&originHeight=1022&originWidth=963&originalType=binary&ratio=1&rotation=0&showTitle=false&size=210336&status=done&style=none&taskId=u9ffcec07-544f-4ab6-91a9-bb33790b377&title=&width=770.4)]

10、启动应用,查看效果

VIDEO1.gif

猜你喜欢

转载自blog.csdn.net/m0_62786921/article/details/128361692