在Android應(yīng)用中,設(shè)置日期和時間是非常簡單的事,由日期和時間對話框控件搞定。
創(chuàng)新互聯(lián)建站堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:做網(wǎng)站、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的金口河網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!一、界面
1、打開“src/com.genwoxue.datetimediy/active_main.java”文件。
然后輸入以下代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:cursorVisible="true" >
</EditText>
<Button
android:id="@+id/dateBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/date" /> <!-- fill_parent寬度填充整個屏幕 -->
<Button
android:id="@+id/timeBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/time" />
<DigitalClock
android:id="@+id/digitalClock"
android:layout_width="fill_parent"
android:textSize="20sp"
android:gravity="center"
android:layout_height="wrap_content" />
<AnalogClock
android:id="@+id/analogClock"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
2、打開“src/com.genwoxue.datetimediy/MainActivity.java”文件。
然后輸入以下代碼:
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
public class MainActivity extends Activity {
private Button dateBtn = null;
private Button timeBtn = null;
private EditText et = null;
private final static int DATE_DIALOG = 0;
private final static int TIME_DIALOG = 1;
private Calendar c = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et);
dateBtn = (Button) findViewById(R.id.dateBtn);
timeBtn = (Button) findViewById(R.id.timeBtn);
dateBtn.setOnClickListener(new View.OnClickListener() {//這樣寫不用import android.view.View.OnClickListener;
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG);//廢棄的自帶方法
}
});
timeBtn.setOnClickListener(new OnClickListener() {//這樣寫要import android.view.View.OnClickListener;
@Override
public void onClick(View arg0) {
showDialog(TIME_DIALOG);
}
});
}
//創(chuàng)建日期及時間選擇對話框
protected Dialog onCreateDialog(int id){
Dialog dialog = null;
switch(id){
case DATE_DIALOG:
c = Calendar.getInstance();
dialog = new DatePickerDialog(this,new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker dp, int year, int month, int dayOfMonth) {
et.setText("您選擇了:"+year+"年"+(month+1)+"月"+dayOfMonth+"日");
}
}, c.get(Calendar.YEAR),//傳入年份
c.get(Calendar.MONTH),//傳入月份
c.get(Calendar.DAY_OF_MONTH));//傳入天數(shù)
break;
case TIME_DIALOG:
c = Calendar.getInstance();
dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
et.setText("您選擇了:"+hourOfDay+"時"+minute+"分");
}
}, c.get(Calendar.HOUR_OF_DAY),
c.get(Calendar.MINUTE),
false);
break;
}
return dialog;
}
}
2、運行,顯示界面:
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享標題:日期對話框DatePickerDialog-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://www.rwnh.cn/article44/csjeee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、域名注冊、外貿(mào)建站、全網(wǎng)營銷推廣、面包屑導(dǎo)航、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容