android13打基础:控件datepicker

datepicker测试activity

java 复制代码
// 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 复制代码
<?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>

点击日期选择器

相关推荐
用户2018792831673 分钟前
浅谈画框ImageView的background和src属性的差异
android
2501_9159090621 分钟前
iOS 加固工具实战解析,主流平台审核机制与工具应对策略
android·ios·小程序·https·uni-app·iphone·webview
马 孔 多 在下雨1 小时前
安卓服务与多线程
android
2501_915106322 小时前
iOS WebView 调试实战,第三方脚本加载失败与内容安全策略冲突问题排查指南
android·ios·小程序·https·uni-app·iphone·webview
消失的旧时光-19433 小时前
Android 键盘
android·键盘监听
Jackilina_Stone17 小时前
【faiss】用于高效相似性搜索和聚类的C++库 | 源码详解与编译安装
android·linux·c++·编译·faiss
棒棒AIT17 小时前
mac 苹果电脑 Intel 芯片(Mac X86) 安卓虚拟机 Android模拟器 的救命稻草(下载安装指南)
android·游戏·macos·安卓·mac
fishwheel18 小时前
Android:Reverse 实战 part 2 番外 IDA python
android·python·安全
消失的旧时光-194320 小时前
Android网络框架封装 ---> Retrofit + OkHttp + 协程 + LiveData + 断点续传 + 多线程下载 + 进度框交互
android·网络·retrofit