【Android开发】小白入门必看的”四框“使用教程,你学废了嘛?

【摘要】 Android开发基础入门、进阶系列文章正陆续和大家发布分享,感兴趣的小伙伴可以关注博主!一起探讨和学习Android技术,助你在编程道路上一臂之力!目录一、RadioButton单选框二、CheckBox复选框三、Spinner下拉框四、ListView列表框五、在xml文件中为下拉框和列表框设置参数Hello,你好呀,我是灰小猿,一个超会写bug的程序猿!最近在进行Android方向的学…

Android开发基础入门、进阶系列文章正陆续和大家发布分享,感兴趣的小伙伴可以关注博主!一起探讨和学习Android技术,助你在编程道路上一臂之力!

目录

一、RadioButton单选框

二、CheckBox复选框

三、Spinner下拉框

四、ListView列表框

五、在xml文件中为下拉框和列表框设置参数


Hello,你好呀,我是灰小猿,一个超会写bug的程序猿!

最近在进行Android方向的学习,所以今天在这里和大家总结一下在Android开发中最经常使用的单选框、复选框、下拉框、列表框的详细使用教程,

之后还会更新更多有关Android入门的技术供大家学习,所以欢迎小伙伴们关注我一起学习呀!

一、RadioButton单选框

单选框RadioButton的使用是建立在RadioGroup中的,原因是因为我们知道单选框的选择是互斥的,也就是说只能选择一个选项,那么如何做到单选框选项的互斥呢?

RadioGroup就起到了作用,在RadioGroup中的单选框选项RadioButton会自动形成互斥,以至于在其中的选项执行选择一个。

判断某个单选框是否被选中使用的是isChecked()方法,当该单选框被选中的时候返回true,否则返回false。

下面我们通过一个实际的案例来介绍单选框的具体使用,选择性别并通过按钮提交之后,在后台可以获取到选中的内容,并返回前端界面消息框显示选中内容。

在XML文件中建立一个单选框界面,进行性别的选择,大家可以看一下其中的单选框RadioButton是放置在哪里的

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/title1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="单选框使用"
        android:textSize="30dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="20dp"
        />

    <TextView
        android:id="@+id/title_sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="性别:"
        android:textSize="30dp"
        app:layout_constraintTop_toBottomOf="@id/title1"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="30dp"

        />
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@id/title_sex"
        app:layout_constraintBottom_toBottomOf="@id/title_sex"
        android:orientation="horizontal"
        android:layout_marginTop="30dp"

        >

        <RadioButton
            android:id="@+id/man"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text=""
            />
        <RadioButton
            android:id="@+id/woman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text=""
            />
    </RadioGroup>

    <Button
        android:id="@+id/tijiao"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"
        android:textSize="30dp"
        android:layout_marginTop="30dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/title_sex"
        ></Button>

</androidx.constraintlayout.widget.ConstraintLayout>

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享