首先是用TimePicker和DatePicker来实现动态调整时间和日期,举例如下:

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class A02Activity extends Activity {
private int ayear,amonth,aday,ahour,aminute;
private TimePicker tp;
private DatePicker dp;
private TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Calendar c=Calendar.getInstance();
ayear=c.get(Calendar.YEAR);
amonth=c.get(Calendar.MONTH);
aday=c.get(Calendar.DAY_OF_MONTH);
ahour=c.get(Calendar.HOUR_OF_DAY);
aminute=c.get(Calendar.MINUTE);
tv=(TextView)findViewById(R.id.tv);
dp=(DatePicker)findViewById(R.id.dp);
dp.init(ayear, amonth, aday, new DatePicker.OnDateChangedListener() {

@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
ayear=year;
amonth=monthOfYear;
aday=dayOfMonth;
updateDisplay();
}
});
tp=(TimePicker)findViewById(R.id.tp);
tp.setIs24HourView(true);
tp.setOnTimeChangedListener(new OnTimeChangedListener(){

@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
ahour=hourOfDay;
aminute=minute;
updateDisplay();
}

});
}
public void updateDisplay(){
tv.setText(new StringBuilder().append(ayear).append("/")
.append(format(amonth+1)).append("/")
.append(format(aday)).append(" ")
.append(format(ahour)).append(":")
.append(format(aminute)));
}
public String format(int x){
String s=""+x;
if(s.length()==1){
s="0"+s;
}
return s;
}
}

其中res/layout/main.xml为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<DatePicker
android:id="@+id/dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TimePicker
android:id="@+id/tp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

</LinearLayout>

从上面的例子可以看出TimePicker的setOnTimeChangedListener()和DatePicker的setOnDateChangedListener()方法是不太一样的,TimePicker的是

直接以setOnTimeChangedListener()方法来处理时间动态改变时所要做的操作;而DatePicker是以init()方法来初始DatePicker的年、月、日和setOnDateChangedListener()处理日期动态改变时所要做的操作的。

在旧版本的Android SDK(1.0r2以前的SDK版本)中,DatePicker对象有提供setOnDateChangedListener()这个方法,但是在新版本的SDK(1.0r2),这个方法被删除了,,所以要实现OnDateChangedListener()时,必须以init()方式来重写OnDateChangedListener();而TimePicker则直接以setOnTimeChangedListener()来实现即可。

用TimePickerDialog和DatePickerDialog来实现动态调整时间和日期例子如下所示:

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;

public class A03Activity extends Activity {
private Button b01,b02;
private int myear,mmonth,mday,mhour,mminute;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b01=(Button)findViewById(R.id.b01);
b01.setText("设定时间");
b01.setBackgroundColor(Color.BLUE);
b02=(Button)findViewById(R.id.b02);
b02.setText("设定日期");
b02.setBackgroundColor(Color.GREEN);


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);

b01.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new TimePickerDialog(A03Activity.this,new OnTimeSetListener(){

@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
// TODO Auto-generated method stub
mhour=hourOfDay;
mminute=minute;
}

},mhour,mminute,true).show();
}

});
b02.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(A03Activity.this,new OnDateSetListener(){

@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub
myear=year;
mmonth=monthOfYear;
mday=dayOfMonth;
}

},myear,mmonth,mday).show();
}

});
}
}

其中res/layout/main.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/b01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/b02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

</LinearLayout>

DatePickerDialog和TimePickerDialog是Android提供的另外两种来实现动态修改日期时间的功能:DatePicker与TimePicker是直接显示在屏幕画面上,而DatePickerDialog与TimePickerDialog对象则是弹出Dialog的方式来显示。

这两种方式都可以实现动态修改日期时间的功能。

更多相关文章

  1. Python3原生编写月份计算工具
  2. Android(安卓)Processdialog 用法
  3. Android通知及receiver
  4. Android实现ListView数据动态加载的方法
  5. Android动态设置主题样式
  6. Android中简单的日期格式化
  7. Android中再按一次退出实现
  8. Android启动流程简析(三)
  9. Android判断两个时间的间隔

随机推荐

  1. ubuntu下adb不能检测到android手机
  2. 总结Android多分辨率支持
  3. 浅入浅出Android(015):使用ImageView显示网
  4. android 所有焦点问题汇总【Focus】
  5. 转:Android 对话框【Dialog】去除白色边框
  6. Android 开发实践 ViewGroup 实现左右滑
  7. 14、到底改如何区分android的平板、电视
  8. android apk重签名命令
  9. Android Https请求详细demo
  10. Android Studio中的build.gradle文件解析