之前的:
舊版時間控件博客-王睿
替換后:
是不是感覺更加美觀些,其實代碼比之前的還要很簡單,大家往下看:
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();}});}
}
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态