一个日期选择对话框
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
Java代码 package com.pandy.db.activity; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Handler; import android.os.Message; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.CalendarView; import android.widget.EditText; import com.pandy.db.R; import com.pandy.db.utils.ToastUtils; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * Created with IntelliJ IDEA. * User: pandy * Date: 13-6-27 * Time: 下午3:33 * To change this template use File | Settings | File Templates. * 传入要现实值的输入框,然后用线程去设定这个值. */ public class CalendarViewDialog { private Activity activity; private EditText in_date; private Date date; public CalendarViewDialog(Activity activity, EditText in_date) { this.activity = activity; this.in_date = in_date; } public void show() { //初始化界面 LayoutInflater factory = LayoutInflater.from(activity); View view = factory.inflate(R.layout._calendar_dialog, null); //创建对话框 final AlertDialog dialog = new AlertDialog.Builder(activity) .setView(view) .create(); //日历事件 CalendarView dialog_calendarView = (CalendarView)view.findViewById(R.id.dialog_calendarView); dialog_calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() { @Override public void onSelectedDayChange(CalendarView calendarView, int i, int i2, int i3) { date = new java.sql.Date(calendarView.getDate()); //启动线程去重画EditText控件 final CalendarDialogHandler handler = new CalendarDialogHandler(); Thread thread = new Thread(new Runnable() { @Override public void run() { Message message = new Message(); message.what=0; handler.sendMessage(message); } }); thread.start(); } }); Button dialog_ok = (Button)view.findViewById(R.id.dialog_ok); dialog_ok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog.dismiss(); } }); dialog.show(); } //重画EditText控件的Handler class CalendarDialogHandler extends Handler { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(0==msg.what){ in_date.setText(date.toString()); } } } } |
