您好,登錄后才能下訂單哦!
一、簡(jiǎn)介
二、方法
最日常的使用方法了
日期控件DatePicker
時(shí)間控件TimePicker
月份從0開(kāi)始
三、代碼實(shí)例
效果圖:
代碼:
fry.Activity01
package fry; import com.example.DatePicherDemo1.R; import android.app.Activity; import android.os.Bundle; import android.widget.DatePicker; import android.widget.DatePicker.OnDateChangedListener; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; import android.widget.Toast; public class Activity01 extends Activity implements OnDateChangedListener,OnTimeChangedListener{ private DatePicker datePicker1; private TimePicker timePicker1; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity01); setTitle("時(shí)間日期控件測(cè)試"); datePicker1=(DatePicker) findViewById(R.id.datePicker1); timePicker1=(TimePicker) findViewById(R.id.timePicker1); //初始化日期,并設(shè)置日期被改變后的監(jiān)聽(tīng)事件 datePicker1.init(2017, 8, 7, this); //設(shè)置時(shí)間以24小時(shí)制 timePicker1.setIs24HourView(true); //設(shè)置時(shí)間被改變后的監(jiān)聽(tīng)時(shí)間 timePicker1.setOnTimeChangedListener(this); } @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub Toast.makeText(this, "日期被改變?yōu)椋?"+year+"."+(monthOfYear+1)+"."+dayOfMonth, Toast.LENGTH_SHORT).show(); } @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub Toast.makeText(this, "時(shí)間被改變?yōu)椋?"+hourOfDay+":"+minute, Toast.LENGTH_SHORT).show(); } }
/DatePicherDemo1/res/layout/activity01.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" > <DatePicker android:id="@+id/datePicker1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TimePicker android:id="@+id/timePicker1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
四、收獲
1、初始化日期,并設(shè)置日期被改變后的監(jiān)聽(tīng)事件
datePicker1.init(2017, 8, 7, this);
2、設(shè)置時(shí)間以24小時(shí)制
timePicker1.setIs24HourView(true);
3、設(shè)置時(shí)間被改變后的監(jiān)聽(tīng)事件
timePicker1.setOnTimeChangedListener(this);
總結(jié)
以上所述是小編給大家介紹的Android開(kāi)發(fā)中DatePicker日期與時(shí)間控件,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。