您好,登錄后才能下訂單哦!
在Android應用中,設置日期和時間是非常簡單的事,由日期和時間對話框控件搞定。
一、界面
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);
}
});
}
//創建日期及時間選擇對話框
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));//傳入天數
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、運行,顯示界面:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。