当前位置: 首页 > news >正文

android13打基础:控件datepicker

datepicker测试activity

// todo: 高级控件日期选择datepicker
public class Ch4_DatePickerActivity extends AppCompatActivity
        implements View
        .OnClickListener, DatePickerDialog.OnDateSetListener {

    private TextView tv_date; // 声明一个文本视图对象
    private DatePicker dp_date; // 声明一个日期选择器对象

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_datepicker_ch4);


        tv_date = findViewById(R.id.ch4_tv_date);
        dp_date = findViewById(R.id.ch4_dp_date);
        // 按钮点击事件
       findViewById(R.id.ch4_btn_date).setOnClickListener(this);
       findViewById(R.id.ch4_btn_ok).setOnClickListener(this);
    }

    @Override // 一旦点击日期对话框上的确定按钮,就会触发监听器的onDateSet方法
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        // 获取日期对话框设定的年月份
        String desc = String.format("您选择的日期是%d年%d月%d日",
                year, month + 1, dayOfMonth);
        tv_date.setText(desc);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ch4_btn_date) {
            // 获取日历的一个实例,里面包含了当前的年月日
            Calendar calendar = Calendar.getInstance();
            // 构建一个日期对话框,该对话框已经集成了日期选择器。
            // DatePickerDialog的第二个构造参数指定了日期监听器
            DatePickerDialog dialog = new DatePickerDialog(this, this,
                    calendar.get(Calendar.YEAR), // 年份
                    calendar.get(Calendar.MONTH), // 月份
                    calendar.get(Calendar.DAY_OF_MONTH)); // 日子
            dialog.show(); // 显示日期对话框
        } else if (v.getId() == R.id.ch4_btn_ok) {
            // 获取日期选择器dp_date设定的年月份
            String desc = String.format("您选择的日期是%d年%d月%d日",
                    dp_date.getYear(), dp_date.getMonth() + 1, dp_date.getDayOfMonth());
            tv_date.setText(desc);
        }
    }
}


布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/ch4_btn_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请选择日期"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <!-- datePickerMode取值spinner表示下拉框风格,取值calendar表示日历风格 -->
    <DatePicker
        android:id="@+id/ch4_dp_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:calendarViewShown="false"
        android:datePickerMode="spinner"
        android:gravity="center"
        android:spinnersShown="true" />

    <Button
        android:id="@+id/ch4_btn_ok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="确  定"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <TextView
        android:id="@+id/ch4_tv_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:textColor="@color/black"
        android:textSize="17sp" />


</LinearLayout>

点击日期选择器

相关文章:

  • 【代码分享】基于IRM和RRT*的无人机路径规划方法详解与Matlab实现
  • 中科大 计算机网络组成原理 1.4 接入网和物理媒体 笔记
  • 公开笔记:Python语法和常用函数介绍
  • C语音的常见概念
  • C语言-----扫雷游戏
  • PHP fastadmin 学习
  • 【CPP面经】科大讯飞 腾讯后端开发面经分享
  • transformer架构解析{掩码,(自)注意力机制,多头(自)注意力机制}(含代码)-3
  • 机器人训练环境isaac gym以及legged_gym项目的配置问题
  • OpenGL 3D纹理
  • Docker新手入门(持续更新中)
  • C#基础及标准控件的使用,附登录案例
  • Docker安装milvus及其基本使用说明
  • IO学习day2
  • 【html期末作业网页设计】
  • 分析白屏winscope
  • AJAX介绍
  • 2024 年 MySQL 8.0.40 安装配置、Workbench汉化教程最简易(保姆级)
  • c++ std::bind、std::bind_front使用笔记
  • CSS—元素水平居中:2分钟掌握常用的水平居中
  • 浙江省建设投资集团有限公司网站/江门关键词优化公司
  • 做医疗网站建设/在线建站平台免费建网站
  • 扬州住房和建设局网站/代发百度首页排名
  • 建设银行官网招聘网站/摘抄一篇新闻
  • 新万网站建设/seo指的是什么意思
  • 高校网站建设研究意义/湖南企业竞价优化服务