import java.util.Calendar; |
import android.app.Activity; |
import android.os.Bundle; |
import android.widget.TextView; |
import android.widget.DatePicker; |
import android.widget.TimePicker; |
public class DateAndTime extends Activity { |
private int mYear; |
private int mMonth; |
private int mDay; |
private int mHour; |
private int mMinute; |
TextView tv; |
TimePicker tp; |
DatePicker dp; |
/** Called when the activity is first created. */ |
@Override |
public void onCreate(Bundle savedInstanceState) { // 取得目前日期与时间 |
Calendar c = Calendar.getInstance(); |
mYear = c.get(Calendar.YEAR); |
mMonth = c.get(Calendar.MONTH); |
mDay = c.get(Calendar.DAY_OF_MONTH); |
mHour = c.get(Calendar.HOUR_OF_DAY); |
mMinute = c.get(Calendar.MINUTE); |
super .onCreate(savedInstanceState); // 载入main.xml Layout |
setContentView(R.layout.main); // 取得TextView对象,并调用updateDisplay()来设定显示的初始日期时间 |
tv = (TextView) findViewById(R.id.showTime); |
updateDisplay(); // 取得DatePicker对象,以init()设定初始值与onDateChangeListener() |
dp = (DatePicker) findViewById(R.id.dPicker); |
dp.init(mYear, mMonth, mDay, new DatePicker.OnDateChangedListener() { |
@Override |
public void onDateChanged(DatePicker view, int year, |
int monthOfYear, int dayOfMonth) { |
mYear = year; |
mMonth = monthOfYear; |
mDay = dayOfMonth; // 调用updateDisplay()来改变显示日期 |
updateDisplay(); |
} |
}); // 取得TimePicker对象,并设定为24小时制显示 |
tp = (TimePicker) findViewById(R.id.tPicker); |
tp.setIs24HourView( true ); // setOnTimeChangedListener,并重写onTimeChanged |
// event |
tp.setOnTimeChangedListener( new TimePicker.OnTimeChangedListener() { |
@Override |
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { |
mHour = hourOfDay; |
mMinute = minute; // 调用updateDisplay()来改变显示时间 |
updateDisplay(); |
} |
}); |
} |
/* 设定显示日期时间的方法 */ |
private void updateDisplay() { |
tv.setText( new StringBuilder().append(mYear).append( "/" ) |
.append(format(mMonth + 1 )).append( "/" ).append(format(mDay)) |
.append( " " ).append(format(mHour)).append( ":" ) |
.append(format(mMinute))); |
} |
/* 日期时间显示两位数的方法 */ |
private String format( int x) { |
String s = "" + x; |
if (s.length() == 1 ) |
s = "0" + s; |
return s; |
} |
} |
main.xml |
---------------------------------------------- |
<?xml version= "1.0" encoding= "utf-8" ?> |
<AbsoluteLayout |
android:id= "@+id/layout1" |
android:layout_width= "fill_parent" |
android:layout_height= "fill_parent" |
xmlns:android= "http://schemas.android.com/apk/res/android" |
> |
<TextView |
android:id= "@+id/mText" |
android:layout_width= "270px" |
android:layout_height= "40px" |
android:text= "@string/str_title" |
android:textSize= "18sp" |
android:layout_x= "20px" |
android:layout_y= "32px" |
/> |
<ImageView |
android:id= "@+id/mImage01" |
android:layout_width= "71px" |
android:layout_height= "96px" |
android:layout_x= "20px" |
android:layout_y= "122px" |
android:src= "@drawable/p04" |
/> |
<ImageView |
android:id= "@+id/mImage02" |
android:layout_width= "71px" |
android:layout_height= "96px" |
android:layout_x= "126px" |
android:layout_y= "122px" |
android:src= "@drawable/p04" |
/> |
<ImageView |
android:id= "@+id/mImage03" |
android:layout_width= "71px" |
android:layout_height= "96px" |
android:layout_x= "232px" |
android:layout_y= "122px" |
android:src= "@drawable/p04" |
/> |
<Button |
android:id= "@+id/mButton" |
android:layout_width= "118px" |
android:layout_height= "wrap_content" |
android:text= "@string/str_button" |
android:layout_x= "100px" |
android:layout_y= "302px" |
/> |
</AbsoluteLayout> |
高级设计师
by: 神马 发表于:2012-09-05 17:22:02 顶(0) | 踩(0) 回复
如果需要弹出对话框选择时间日期,则需要用DatePickerDialog和TimePickerDialog组件。
回复评论