1.布局common_time.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="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TimePicker
android:id="@+id/timepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner"/>
</LinearLayout>
2.MyTimePickerDialog.class
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
public class MyTimePickerDialog implements OnTimeChangedListener {
private Context mContext;
private AlertDialog.Builder b;
private TimePicker tp;
private static boolean is24Hour = true;
private OnTimeChangeListener listener;
private String hour = "00";
private String minute = "00";
private int hourI = 0;
private int minuteI = 0;
private static String[] displayvalue = new String[] { "00", "30" };//自定义分钟选择为 00 和 30
public MyTimePickerDialog(Context context) {
this(context, is24Hour, 0, 0);
}
public MyTimePickerDialog(Context context, boolean is24Hour, int hour,
int minute) {
this.is24Hour = is24Hour;
this.mContext = context;
this.hourI = hour;
this.minuteI = minute;
init();
}
private void init() {
View view = LayoutInflater.from(mContext).inflate(R.layout.common_time,
null);
tp = (TimePicker) view.findViewById(R.id.timepicker);
tp.setIs24HourView(is24Hour);
tp.setCurrentHour(hourI);
tp.setCurrentMinute(minuteI);
NumberPicker mMinuteSpinner = findNumberPicker(tp).get(
findNumberPicker(tp).size() - 1);//得到分钟的NumberPicker组件
//设置分钟组件的参数
mMinuteSpinner.setMinValue(0);//最小0
mMinuteSpinner.setMaxValue(1);//最大1
mMinuteSpinner.setDisplayedValues(displayvalue);
mMinuteSpinner.setOnLongPressUpdateInterval(100);
tp.setOnTimeChangedListener(this);
b = new AlertDialog.Builder(mContext);
b.setView(view);
b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (listener != null) {
listener.onTimeChange(hour, minute);
}
}
});
b.setNegativeButton("取消", null);
b.create().show();
}
/**
* 得到viewGroup里面的numberpicker组件
*
* @param viewGroup
* @return
*/
private List<NumberPicker> findNumberPicker(ViewGroup viewGroup) {
List<NumberPicker> npList = new ArrayList<NumberPicker>();
View child = null;
if (null != viewGroup) {
for (int i = 0; i < viewGroup.getChildCount(); i++) {
child = viewGroup.getChildAt(i);
if (child instanceof NumberPicker) {
npList.add((NumberPicker) child);
} else if (child instanceof LinearLayout) {
List<NumberPicker> result = findNumberPicker((ViewGroup) child);
if (result.size() > 0) {
return result;
}
}
}
}
return npList;
}
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
this.hour = hourOfDay < 10 ? "0" + hourOfDay : "" + hourOfDay;
this.minute = displayvalue[minute];
}
public void setOnTimeChangeListener(OnTimeChangeListener listener) {
this.listener = listener;
}
public interface OnTimeChangeListener {
/**
*返回字符串 hour:HH minute:mm
*/
void onTimeChange(String hour, String minute);
}
}
3.附修改NumberPicker属性的方法
/*
* 调整numberpicker大小
*/
private void resizeNumberPicker(NumberPicker np) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ScreenUtil.dp2px(mContext, 50.0f),
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10, 0, 10, 0);
np.setLayoutParams(params);
}
/**
* ScreenUtil.class中dp转px的方法
* @param context
* @param dp
* @return dp转换为px
*/
public static int dp2px(Context context,float dp){
float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp*scale);
}