在此处可完全不用Handler,只是了解用法,Handler是为了解决android的多线程问题--Android平台下不允许新启动的线程访问该Activity里面的组件,这样会导致新启动的线程无法动态改变界面组件中的属性值,这就需要Handler的消息传递机制来解决了。
--》1.在新启动的线程中发送消息。(当然啦,这里并非新启动的线程)
Message msg = new Message();
msg.what = DATE_ID;
myHandler.sendMessage(msg);
-->2.在主线程中获取、处理消息。
private static int DATE_ID = 0;
private static int TIME_ID = 1;
Handler myHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what){
case 0:
setCurrentDate(year, month, day);
break;
case 1:
setCurrentTime(hour, minute);
break;
}
}
};
小实例的代码如下:
public class MainActivity extends Activity implements OnClickListener{
private EditText showDate, showTime;
private Button pickDate, pickTime;
private int year, month, day, hour, minute;
private static int DATE_ID = 0;
private static int TIME_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showDate = (EditText)findViewById(R.id.showdate);
showTime = (EditText)findViewById(R.id.showtime);
pickDate = (Button)findViewById(R.id.pickdate);
pickTime = (Button)findViewById(R.id.picktime);
pickDate.setOnClickListener(this);
pickTime.setOnClickListener(this);
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
setCurrentDate(year, month, day);
setCurrentTime(hour, minute);
}
public void setCurrentDate(int year, int month, int day){
showDate.setText(year + "-" + (month+1) + "-" + day);
}
public void setCurrentTime(int hour, int minute){
showTime.setText(hour + ":" + minute);
}
@Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
switch(v.getId()){
case R.id.pickdate:
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
new DatePickerDialog(
MainActivity.this,
new OnDateSetListener() {
// DatePickerDialog5个参数.1:上下文,2:设置日期的回调.
//可以在里面调用更新ui的方法更新ui,3:45就是初始化时候的年月日了.
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
MainActivity.this.year = year;
MainActivity.this.month = monthOfYear;
MainActivity.this.day = dayOfMonth;
Message msg = new Message();
msg.what = DATE_ID;
myHandler.sendMessage(msg);
}
}, year, month, day).show();
break;
case R.id.picktime:
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
new TimePickerDialog(
MainActivity.this,
new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
MainActivity.this.hour = hourOfDay;
MainActivity.this.minute = minute;
Message msg = new Message();
msg.what = TIME_ID;
myHandler.sendMessage(msg);
}
}, hour, minute, true).show();
break;
}
}
Handler myHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what){
case 0:
setCurrentDate(year, month, day);
break;
case 1:
setCurrentTime(hour, minute);
break;
}
}
};
}
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="欢迎关注Aaron.Ou Blog" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="日期和时间控件的使用DEMO" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/showdate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <Button android:id="@+id/pickdate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择日期"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/showtime" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <Button android:id="@+id/picktime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择时间"/> </LinearLayout> </LinearLayout>