android使用NumberPicker实现自定义带时间的对话框

卫财
2023-12-01
android使用NumberPicker实现自定义带时间的对话框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal" >

        <NumberPicker
            android:id="@+id/year"
            android:layout_width="50dip"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:visibility="gone" />

        <TextView
            android:id="@+id/year_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="年"
            android:visibility="gone" />

        <NumberPicker
            android:id="@+id/hour"
            android:layout_width="50dip"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:visibility="gone" />

        <TextView
            android:id="@+id/hour_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="时"
            android:visibility="gone" />

        <NumberPicker
            android:id="@+id/min"
            android:layout_width="50dip"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:visibility="gone" />

        <TextView
            android:id="@+id/min_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="分"
            android:visibility="gone" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/sure_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="确定" />

        <Button
            android:id="@+id/cacel_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:text="取消" />
    </LinearLayout>

</LinearLayout>


java代码
package numberpicker;

import java.util.Calendar;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.NumberPicker.Formatter;
import android.widget.NumberPicker.OnScrollListener;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.TextView;

import com.example.numberwheeltest.R;

public class NumberDialog extends Dialog implements OnValueChangeListener,
		OnScrollListener, Formatter, android.view.View.OnClickListener {
	private NumberPicker year_numberpicker;// 年
	private NumberPicker hour_numberpicker;// 时
	private NumberPicker min_numberpicker;// 分
	private TextView year_tv;
	private TextView hour_tv;
	private TextView min_tv;

	public static int YEAR = 1;// 年
	public static int MONTH = 2;// 月
	public static int DATE = 3;// 日
	public static int YEAR_DATE = 4;// 月日
	public static int YEAR_MONTH_DATE = 5;// 年月日
	public static int HOUR_MIN_SECOND = 6;// 时分秒
	private int dateFormat = YEAR_MONTH_DATE;// 月日,时分秒

	private Context context;
	private Button sure_btn, cacel_btn;
	private OnclickLinsterDialog linsterDialog;

	public NumberDialog(Context context) {
		super(context);
		this.context = context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.datetime_picker);

		Window dialogWindow = getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = getWidth() * 9 / 10; // 宽度
		lp.height = WindowManager.LayoutParams.WRAP_CONTENT; // 高度
		dialogWindow.setAttributes(lp);

		setTitle("选择时间");
		initView();

		super.onCreate(savedInstanceState);
	}

	public void setDateFormat(int format) {
		this.dateFormat = format;

	}

	/**
	 * 获取手机屏幕宽
	 * 
	 * @return
	 */
	public int getWidth() {
		int width = 1;
		WindowManager wm = (WindowManager) getContext().getSystemService(
				Context.WINDOW_SERVICE);
		width = wm.getDefaultDisplay().getWidth();
		return width;
	}

	private void initView() {
		sure_btn = (Button) findViewById(R.id.sure_btn);
		cacel_btn = (Button) findViewById(R.id.cacel_btn);
		sure_btn.setOnClickListener(this);
		cacel_btn.setOnClickListener(this);

		year_tv = (TextView) findViewById(R.id.year_tv);
		hour_tv = (TextView) findViewById(R.id.hour_tv);
		min_tv = (TextView) findViewById(R.id.min_tv);

		Calendar calendar = Calendar.getInstance();

		year_numberpicker = (NumberPicker) findViewById(R.id.year);
		year_numberpicker.setFormatter(this);
		year_numberpicker.setOnValueChangedListener(this);// 值改变监听
		year_numberpicker.setOnScrollListener(this);// 活动监听
		year_numberpicker.setEnabled(true);// 内容不可编辑

		hour_numberpicker = (NumberPicker) findViewById(R.id.hour);
		hour_numberpicker.setFormatter(this);
		hour_numberpicker.setOnValueChangedListener(this);// 值改变监听
		hour_numberpicker.setOnScrollListener(this);// 活动监听
		hour_numberpicker.setEnabled(true);// 内容不可编辑

		min_numberpicker = (NumberPicker) findViewById(R.id.min);
		min_numberpicker.setFormatter(this);
		min_numberpicker.setOnValueChangedListener(this);
		min_numberpicker.setOnScrollListener(this);
		hour_numberpicker.setEnabled(true);

		if (dateFormat == YEAR) {// 年
			year_numberpicker.setMaxValue(2100);// 最大值
			year_numberpicker.setMinValue(1900);// 最小值
			year_numberpicker.setValue(calendar.get(Calendar.YEAR));// 获取当前年
			year_numberpicker.setVisibility(View.VISIBLE);
			year_tv.setVisibility(View.VISIBLE);

		} else if (dateFormat == MONTH) {// 月
			hour_numberpicker.setMaxValue(12);// 最大值
			hour_numberpicker.setMinValue(1);// 最小值
			hour_numberpicker.setValue(calendar.get(Calendar.MONTH));
			hour_numberpicker.setVisibility(View.VISIBLE);
			hour_tv.setVisibility(View.VISIBLE);

		} else if (dateFormat == DATE) {// 日
			min_numberpicker.setMaxValue(31);
			min_numberpicker.setMinValue(1);
			min_numberpicker.setValue(calendar.get(Calendar.DATE));
			min_numberpicker.setVisibility(View.VISIBLE);
			min_tv.setVisibility(View.VISIBLE);

		} else if (dateFormat == YEAR_MONTH_DATE) {// 年月日
			// 年
			year_numberpicker.setMaxValue(2100);// 最大值
			year_numberpicker.setMinValue(1990);// 最小值
			year_numberpicker.setValue(calendar.get(Calendar.YEAR));// 获取当前年
			// 月
			hour_numberpicker.setMaxValue(12);// 最大值
			hour_numberpicker.setMinValue(1);// 最小值
			hour_numberpicker.setValue(calendar.get(Calendar.MONTH) + 1);// 获取当前月
			// 日
			min_numberpicker.setMaxValue(31);
			min_numberpicker.setMinValue(1);
			min_numberpicker.setValue(calendar.get(Calendar.DATE));// 获取当前年

			year_numberpicker.setVisibility(View.VISIBLE);
			hour_numberpicker.setVisibility(View.VISIBLE);
			min_numberpicker.setVisibility(View.VISIBLE);
			year_tv.setVisibility(View.VISIBLE);
			year_tv.setText("年");
			hour_tv.setVisibility(View.VISIBLE);
			hour_tv.setText("月");
			min_tv.setVisibility(View.VISIBLE);
			min_tv.setText("日");
		} else if (dateFormat == HOUR_MIN_SECOND) {
			// 时
			year_numberpicker.setMaxValue(24);// 最大值
			year_numberpicker.setMinValue(0);// 最小值
			year_numberpicker.setValue(calendar.get(Calendar.HOUR_OF_DAY));// 获取当前时
			// 分
			hour_numberpicker.setMaxValue(60);// 最大值
			hour_numberpicker.setMinValue(0);// 最小值
			hour_numberpicker.setValue(calendar.get(Calendar.MINUTE));// 获取当前时
			// 秒
			min_numberpicker.setMaxValue(60);
			min_numberpicker.setMinValue(0);
			min_numberpicker.setValue(calendar.get(Calendar.SECOND));

			year_numberpicker.setVisibility(View.VISIBLE);
			hour_numberpicker.setVisibility(View.VISIBLE);
			min_numberpicker.setVisibility(View.VISIBLE);
			year_tv.setVisibility(View.VISIBLE);
			year_tv.setText("时");
			hour_tv.setVisibility(View.VISIBLE);
			hour_tv.setText("分");
			min_tv.setVisibility(View.VISIBLE);
			min_tv.setText("秒");
		}

	}

	/***
	 * 值改变监听 oldVal 原来值 newVal 改变值
	 */
	@Override
	public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
		switch (picker.getId()) {

		case R.id.year:

			if (newVal < 1990) {
				year_numberpicker.setValue(newVal + 1);
			}
			break;

		case R.id.hour:// 中间的控件,1,显示月,2,显示分

			// 如果显示年月日
			if (dateFormat == YEAR_MONTH_DATE) {
				if (oldVal == 12 && newVal == 01) {
					year_numberpicker
							.setValue(year_numberpicker.getValue() + 1);
				} else if (oldVal == 01 && newVal == 12) {
					year_numberpicker
							.setValue(year_numberpicker.getValue() - 1);
				}

				// 如果显示时分秒
			} else if (dateFormat == HOUR_MIN_SECOND) {
				if (oldVal == 60 && newVal == 01) {
					year_numberpicker
							.setValue(year_numberpicker.getValue() + 1);
				} else if (oldVal == 01 && newVal == 60) {
					year_numberpicker
							.setValue(year_numberpicker.getValue() - 1);
				}
			}

			break;
		case R.id.min:// // 中间的控件,1,显示日,2,显示秒
			// 显示年月日
			if (dateFormat == YEAR_MONTH_DATE) {
				if (oldVal == 31 && newVal == 01) {
					hour_numberpicker
							.setValue(hour_numberpicker.getValue() + 1);
				} else if (newVal == 31 && oldVal == 01) {
					hour_numberpicker
							.setValue(hour_numberpicker.getValue() - 1);
				}
			} else if (dateFormat == HOUR_MIN_SECOND) {
				if (oldVal == 60 && newVal == 01) {
					hour_numberpicker
							.setValue(hour_numberpicker.getValue() + 1);
				} else if (oldVal == 01 && newVal == 60) {
					hour_numberpicker
							.setValue(hour_numberpicker.getValue() - 1);
				}
			}

		default:
			break;
		}
	}

	/***
	 * value的值格式,当值小于10,前面加一个0
	 */
	@Override
	public String format(int value) {
		String tmpStr = String.valueOf(value);
		if (value < 10) {
			tmpStr = "0" + tmpStr;
		}
		return tmpStr;
	}

	/**
	 * 滑动监听事件
	 */
	@Override
	public void onScrollStateChange(NumberPicker view, int scrollState) {

		switch (scrollState) {
		case OnScrollListener.SCROLL_STATE_FLING:// 后续滑动,停不下来
			break;
		case OnScrollListener.SCROLL_STATE_IDLE:// 不滑动
			break;
		case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:// 滑动中
			break;
		}

	}

	public void setOnclickLinsterDialog(OnclickLinsterDialog click) {
		this.linsterDialog = click;
	}

	interface OnclickLinsterDialog {

		void choseDate(String date);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.sure_btn:
			String year = String.valueOf(year_numberpicker.getValue());// 年
			String month = String.valueOf(hour_numberpicker.getValue());// 月
			String date = String.valueOf(min_numberpicker.getValue());// 日
			linsterDialog.choseDate("选择的时间是:" + year + ";month:" + month
					+ ";date:" + date);
			dismiss();

			break;
		case R.id.cacel_btn:
			dismiss();
			break;

		default:
			break;
		}

	}

}


调用:
NumberDialog numberDialog = new NumberDialog(this);
			numberDialog.setDateFormat(NumberDialog.YEAR_MONTH_DATE);
			numberDialog.show();
			numberDialog.setOnclickLinsterDialog(new OnclickLinsterDialog() {

				@Override
				public void choseDate(String date) {
					// TODO Auto-generated method stub
					Toast.makeText(MainActivity.this, date, 1000).show();

				}
			});


 类似资料: