在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。