datepicker設置默認日期,Android DatePicker 時間選擇器

 2023-12-25 阅读 28 评论 0

摘要:之前的: 舊版時間控件博客-王睿 替換后: 是不是感覺更加美觀些,其實代碼比之前的還要很簡單,大家往下看: activity.xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android

之前的:
在這里插入圖片描述
舊版時間控件博客-王睿
替換后:
在這里插入圖片描述

是不是感覺更加美觀些,其實代碼比之前的還要很簡單,大家往下看:

activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"><DatePickerandroid:id="@+id/dp_time"android:layout_marginTop="20dp"android:layout_width="match_parent"android:layout_height="100dp"android:datePickerMode="spinner"android:layout_centerInParent="true"android:calendarViewShown="false"/></RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {private DatePicker dpTime;private String time;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);dpTime = findViewById(R.id.dp_time);//  init方法,第1~3參數分別表示:年,月,日,第四個參數是:一個DatePicker 時間控件的對象//  再調用OnDateChangedListener()方法,就可以達到時刻監聽的效果dpTime.init(2019, 9, 4, new DatePicker.OnDateChangedListener() {@Overridepublic void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {time = ""+year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日";Toast.makeText(MainActivity.this,"時間為:"+time,Toast.LENGTH_LONG).show();}});}
}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/194817.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息