学习Android半年了,一直想写点东西,今天开始写第一篇,目前在弄的一个小项目,是关于课程表智能闹钟,今天做这个TimePickerDialog查了很多资料,做成了下面这个效果,
这里timepicker用的是github一个自定义控件
https://github.com/jingchenUSTC/TimePicker
先上布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="50dp" android:gravity="center"> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center"> <LinearLayout android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <autoalarm.keygod.com.view.PickerView android:id="@+id/hour" android:layout_width="100dp" android:layout_height="80dp" android:layout_weight="1"/> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_centerVertical="true" android:gravity="center" android:text=" : " android:textColor="#ffaa33" android:textSize="30sp" android:textStyle="bold" /> <autoalarm.keygod.com.view.PickerView android:id="@+id/minute" android:layout_width="100dp" android:layout_height="80dp" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:gravity="center" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="70dp" android:layout_height="wrap_content" android:text="@string/queren" android:id="@+id/tp_queren"/> <Button android:layout_width="70dp" android:layout_height="wrap_content" android:text="@string/quxiao" android:id="@+id/tp_quxiao"/> </LinearLayout> </LinearLayout>
最后是Activity中的点击事件
public class MyTimePickerDialog extends Dialog { Context context; PickerView hour_pv, minute_pv; List<String> hour = new ArrayList<String>(); List<String> minute = new ArrayList<String>(); Button tp_queren, tp_quxiao; public static String hourText ="12"; public static String minuteText = "30"; DialogOnClickListener dialogOnClickListener; Dialog dialog; public MyTimePickerDialog(Context context) { super(context); this.context = context; dialog = new Dialog(context, R.style.dialog); dialog.setContentView(R.layout.timepickerdialog); hour_pv = (PickerView) dialog.findViewById(R.id.hour); minute_pv = (PickerView) dialog.findViewById(R.id.minute); tp_queren = (Button) dialog.findViewById(R.id.tp_queren); tp_quxiao = (Button) dialog.findViewById(R.id.tp_quxiao); //设置自定义timepicker的数据 for (int i = 0; i < 10; i++) { hour.add("0" + i); } for (int i1 = 10; i1 < 24; i1++) { hour.add(Integer.toString(i1)); } for (int i = 0; i < 60; i++) { minute.add(i < 10 ? "0" + i : "" + i); } hour_pv.setData(hour); minute_pv.setData(minute); hour_pv.setOnSelectListener(new PickerView.onSelectListener() { @Override public void onSelect(String text) { hourText = text; } }); minute_pv.setOnSelectListener(new PickerView.onSelectListener() { @Override public void onSelect(String text) { minuteText = text; } }); /* *设置按钮点击事件 */ tp_queren.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialogOnClickListener.confirmedDo(hourText, minuteText); dismiss(); } }); tp_quxiao.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); } }); } //按钮点击事件接口,用于在activity中完成功能,传入的是timepicker的hour和minute public interface DialogOnClickListener { public void confirmedDo(String hourText,String minuteText); } public void setDialogOnClickListener(DialogOnClickListener dialogOnClickListener){ this.dialogOnClickListener = dialogOnClickListener; } //以下是一些常用方法 public String getHourText(){ return hourText; } public String getMinuteText(){ return minuteText; } public void show() { dialog.show(); } public void hide(){ dialog.hide(); } public void dismiss(){ dialog.dismiss(); } }
最后是Activity中的点击事件
confirmButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MyTimePickerDialog myDialog = new MyTimePickerDialog(TimePickerActicity.this); myDialog.setDialogOnClickListener(new MyTimePickerDialog.DialogOnClickListener() { @Override public void confirmedDo(String hourText, String minuteText) { timeEndList.get(index).setText(""+hourText+" : "+minuteText+""); TimeModel t = new TimeModel(hourText,minuteText); timeEnds.set(index, t); isOpened[index+10] = "1"; } }); myDialog.show(); } });
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">false</item> <item name="android:windowNoTitle">true</item> <item name="android:background">#FFF</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:backgroundDimEnabled">true</item> </style> </resources>