02-android studio实现下拉列表+单选框+年月日功能

news/2024/7/8 3:39:17 标签: android studio, gitee, android, kotlin

一、下拉列表功能

1.效果图

2.实现过程

1)添加组件

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:padding="10dip">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="身份"
                android:textColor="#FF000000"
                android:textSize="24sp" />
            <Spinner
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:overlapAnchor="false"
                android:spinnerMode="dropdown"
                android:textSize="22sp"
                android:paddingTop="10dip"
                android:id="@+id/userRole"/>
        </LinearLayout>

2)添加适配器

        val userRole:Spinner = findViewById(R.id.userRole)
        val roleData = listOf<String>("患者","咨询师","管理员")
        userRole.setSelection(1)
        val roleAdapter = ArrayAdapter(this,android.R.layout.simple_list_item_1,roleData)
        userRole.adapter=roleAdapter

3.注意

1)遮盖组件

如果在xml中没有定义android:overlapAnchor="false" android:spinnerMode="dropdown"这两行代码,将会出现组件被覆盖的情况

2)去掉顶部导航栏

supportActionBar?.hide()

二、单选框功能

1.效果图

2.实现过程

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:padding="15dip">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="性别"
                android:paddingTop="10dip"
                android:paddingRight="10dip"
                android:gravity="center"
                android:textColor="#FF000000"
                android:textSize="24sp" />
            <RadioGroup
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dip"
                android:id="@+id/sex"
                android:orientation="horizontal">
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="男"
                    android:textSize="24sp"
                    android:id="@+id/sexBoy"
                    android:textColor="#000"/>
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="女"
                    android:textSize="24sp"
                    android:id="@+id/sexGirl"
                    android:textColor="#000"/>
            </RadioGroup>
        </LinearLayout>

三、选择年月日功能

1.效果图

2.实现过程

1)定义组件

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:padding="15dip">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="出生日期"
                android:textColor="#FF000000"
                android:paddingRight="10dip"
                android:textSize="24sp" />
            <EditText
                android:id="@+id/tvDateTime"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="15dip"
                android:hint="点击选择时间"
                android:clickable="true"
                android:focusable="true"
                android:textSize="22sp" />
        </LinearLayout>

2)显示时间

class AddPatient : AppCompatActivity() {

    private lateinit var tvDateTime: EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_addpatient)

        tvDateTime = findViewById(R.id.tvDateTime)
        tvDateTime.setOnClickListener {
            showDatePicker()
        }
    }

    private fun showDatePicker() {
        val calendar = Calendar.getInstance()
        val year = calendar.get(Calendar.YEAR)
        val month = calendar.get(Calendar.MONTH)
        val day = calendar.get(Calendar.DAY_OF_MONTH)

        val datePickerListener = DatePickerDialog.OnDateSetListener { _, selectedYear, selectedMonth, selectedDay ->
            // 使用SimpleDateFormat来格式化日期
            val dateFormatter = SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault())
            val selectedDate = dateFormatter.format(Date(selectedYear - 1900, selectedMonth, selectedDay))
            tvDateTime.setText(selectedDate)
        }

        DatePickerDialog(
            this,
            datePickerListener,
            year,
            month,
            day
        ).show()
    }
}


http://www.niftyadmin.cn/n/5536394.html

相关文章

C++ 的常见算法 之三

C 的常见算法 之三 合并merge使用实列 inplace_merge使用实列 set_difference使用实列 堆make_heap使用实列 sort_heap使用实列 合并 merge 将排序范围 [first1,last1) 和 [first2,last2) 中的元素合并到一个新范围中&#xff0c;该范围从 result 开始&#xff0c;所有元素均…

昇思25天学习打卡营第6天|linchenfengxue

​​​​​​SSD目标检测 SSD&#xff0c;全称Single Shot MultiBox Detector&#xff0c;是Wei Liu在ECCV 2016上提出的一种目标检测算法。使用Nvidia Titan X在VOC 2007测试集上&#xff0c;SSD对于输入尺寸300x300的网络&#xff0c;达到74.3%mAP(mean Average Precision)以…

Dialog设置背景透明和尺寸

class TestDialog(context: Context?,var clickListener: OnClickCallBack) : Dialog(context!!) {lateinit var binding:TestDialogBindingoverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)binding TestDialogBinding.inflate(Lay…

Feign:简化微服务通信的利器

介绍 1.1 什么是 Feign&#xff1f; Feign 是一个声明式、模板化的 HTTP 客户端&#xff0c;它简化了编写 Web 服务客户端的过程。它的主要目的是使 HTTP API 客户端的开发变得更加简单和直观。Feign 的设计理念是将 HTTP 客户端的细节隐藏在背后&#xff0c;使开发者可以专注…

GPT提示词模板

BRTR 原则 # 背景&#xff08;Background&#xff09; - 描述任务的背景信息&#xff0c;包括任务的起因、目的、相关的历史信息或当前状况。 - 提供足够的背景信息以便让ChatGPT理解任务的上下文。 # 角色&#xff08;Role&#xff09; - 定义ChatGPT在任务中所扮演的角色&…

比赛获奖的武林秘籍:01 如何看待当代大学生竞赛中“卷”“祖传老项目”“找关系”的现象?

比赛获奖的武林秘籍&#xff1a;01 如何看待当代大学生竞赛中“卷”“祖传老项目”“找关系”的现象&#xff1f; 摘要 本文主要分析了大学生电子计算机类比赛中“卷”“祖传老项目”“找关系”的现象&#xff0c;结合自身实践经验&#xff0c;给出了相应的解决方案。 正文 …

高级策略:解读 SQL 中的复杂连接

了解基本连接 在深入研究复杂连接之前&#xff0c;让我们先回顾一下基本连接的基础知识。 INNER JOIN&#xff1a;根据指定的连接条件检索两个表中具有匹配值的记录。LEFT JOIN&#xff1a;从左表检索所有记录&#xff0c;并从右表中检索匹配的记录&#xff08;如果有&#x…

Greenplum(一)【MPP 架构 数据类型】

1、Greenplum 入门 Greenplum 是基于 MPP 架构的一款分布式分析型数据库&#xff0c;具备关系型数据库的特点&#xff0c;因为它处理的是结构化的数据&#xff0c;同时具备大数据分布式的特点。 1.1、MPP 架构 MPP&#xff08;Massively Parallel Processing&#xff09;架构是…