时间选择器DatePicker和TimePicker

时间:2022-07-05 18:09:19
        DatePicker是一个日期选择控件,它继承自FrameLayout类,用来实现的主要功能是使用护可以方便选择日期。如果要捕获用户修改DataPicker控件中的数据改变事件,需要为DatePicker添加OnDateChangedListener监听器。

        TimePicker是一个时间选择控件,也继承自FrameLayout类。时间选择控件向用户显示一天中的时间(可以为24小时,也可以为AM/PM制),并允许用户进行选择。如果要捕获用户修改时间数据的事件,便需要为TimePicker添加OnTimeChangedListener监听器。

其中,DatePicker和TimePicker各有两种模式:datePickerMode = “spinner”datePickerMode = “calendar”

                                                                         timePickerMode = “spinner”timePickerMode = “clock”

设置不同的模式,其显示的样式不同。这个可根据个人喜爱设置不同的模式。(本人以spinner为例)

下面是一个Demo列子:

选择弹出框:

public class TimeDialog extends Dialog{

    private TimePicker mTimePicker;
    private Context context;
    private DatePicker mDatePicker;
    private TextView sureText,cancleText;
    private String sureTexting,cancleTexting;
    private ClickListener clickListener;
    public TimeDialog(Context context) {
        super(context);
        init();
    }
    public TimeDialog(Context context, String sureTexting,String cancleTexting) {
        super(context);
        this.context = context;
        this.sureTexting = sureTexting;
        this.cancleTexting = cancleTexting;
        init();
    }

    public void setClickListener(ClickListener clickListener){
        this.clickListener = clickListener;
    }


    private void init() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.time_layout, null);
        setContentView(view);
        mDatePicker = (DatePicker) findViewById(R.id.datepicker);
        mTimePicker = (TimePicker) findViewById(R.id.timePicker);
        sureText = (TextView) findViewById(R.id.text_sure);
        cancleText = (TextView) findViewById(R.id.text_cancle);
        sureText.setText(sureTexting);
        cancleText.setText(cancleTexting);
        sureText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer sb = new StringBuffer();
                sb.append(String.format("%d-%02d-%02d-%02d:%02d",mDatePicker.getYear(),mDatePicker.getMonth() + 1,mDatePicker.getDayOfMonth(),mTimePicker.getCurrentHour(),mTimePicker.getCurrentMinute()));
                clickListener.doConfirm(sb.toString());

            }
        });
        cancleText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickListener.doCancle();
            }
        });
    }
    public interface ClickListener{
        public void doConfirm(String data);
        public void doCancle();
    }
}
Xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="选择日期" />

    <DatePicker
        android:id="@+id/datepicker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:calendarViewShown="false"
        android:datePickerMode="spinner"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="选择时间" />

    <TimePicker
        android:id="@+id/timePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:timePickerMode="spinner"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginBottom="10dp">
        <TextView
            android:id="@+id/text_sure"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"/>
        <TextView
            android:id="@+id/text_cancle"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"/>
    </LinearLayout>

</LinearLayout>
再此,datePicker和timePicker两种的模式我均设置为spinner,有兴趣的可换另外一种模式,看一下效果;

主函数:

  private TextView mTextView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.textView);
    }

    public void btnClick(View view){
        final TimeDialog mTimeDialog = new TimeDialog(this,"确定","取消");
        mTimeDialog.show();
        mTimeDialog.setClickListener(new TimeDialog.ClickListener() {
            @Override
            public void doConfirm(String data) {
                mTextView.setText(data);
                mTimeDialog.dismiss();
            }

            @Override
            public void doCancle() {
                mTextView.setText("没有任何设置");
                mTimeDialog.dismiss();
            }
        });
    }
}
Xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.wls.timepicker.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="当前时间为:"
        android:layout_margin="10dp"
        android:textSize="16sp"/>
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:textSize="16sp"/>
    </LinearLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="btnClick"
        android:text="选择时间"/>
</LinearLayout>
效果展示位:

时间选择器DatePicker和TimePicker  时间选择器DatePicker和TimePicker  时间选择器DatePicker和TimePicker

其中TimePicker可以设置为24小时制只需建设功能一句:

mTimePicker.setIs24HourView(true);
即可。