• 第一阶段冲刺5


    package com.example.zbytexttwo;

    import androidx.appcompat.app.AppCompatActivity;

    import java.util.Calendar;

    import android.os.Bundle;

    import android.widget.DatePicker;

    import android.widget.TextView;

    import android.widget.TimePicker;

    public class MainJmActivity extends AppCompatActivity {

        private TextView textview;

        private TimePicker timepicker;

        private DatePicker datepicker;

        /* 声明日期及时间变量*/

        private int year;

        private int month;

        private int day;

        private int hour;

        private int minute;

        @Override

        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main_jm);

            /*获取当前日期及时间 */

            Calendar calendar = Calendar.getInstance();

            year = calendar.get(Calendar.YEAR);

            month = calendar.get(Calendar.MONTH);

            day = calendar.get(Calendar.DAY_OF_MONTH);

            hour = calendar.get(Calendar.HOUR);

            minute = calendar.get(Calendar.MINUTE);

            datepicker = (DatePicker) findViewById(R.id.datepicker);

            timepicker = (TimePicker) findViewById(R.id.timepicker);

            /* 设置TextView对象,显示初始日期时间 */

            textview = (TextView) findViewById(R.id.timeview);

            textview.setText(new StringBuilder().append(year).append("/")

                    .append(format(month + 1)).append("/")

                    .append(format(day)).append(" ")

                    .append(format(hour)).append(":")

                    .append(format(minute)));

            /* 设置OnDateChangedListener()*/

            datepicker.init(year, month, day,

                    new DatePicker.OnDateChangedListener() {

                        @Override

                        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                            // TODO Auto-generated method stub

                            MainJmActivity.this.year = year;

                            month = monthOfYear;

                            day = dayOfMonth;

                            textview.setText(new StringBuilder().append(year).

                                    append("/").append(format(month + 1))

                                    .append("/").append(format(day)).append(" ")

                                    .append(format(hour)).append(":")

                                    .append(format(minute)));

                        }

                    });

            timepicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

                @Override

                public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

                    // TODO Auto-generated method stub

                    hour = hourOfDay;

                    MainJmActivity.this.minute = minute;

                    textview.setText(new StringBuilder().append(year)

                            .append("/").append(format(month + 1))

                            .append("/").append(format(day)).append(" ")

                            .append(format(hour)).append(":")

                            .append(format(minute)));

                }

            });

        }

        private String format(int time) {

            String str = "" + time;

            if (str.length() == 1)

                str = "0" + str;

            return str;

        }

    }

    MainJmActivity.java

    <?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

        xmlns:tools="http://schemas.android.com/tools"

        android:id="@+id/tab01"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        tools:context=".MainJmActivity"

        android:orientation="vertical">

        <TextView

            android:id="@+id/timeview"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="DatePickerTimePicker"/>

        <TimePicker

            android:id="@+id/timepicker"

            android:layout_width="wrap_content"

            android:layout_height="116dp"

            android:background="#778888"/>

        <!--设置背景色为墨绿-->

        <DatePicker

            android:id="@+id/datepicker"

            android:layout_width="271dp"

            android:layout_height="196dp"

            android:background="#778899"/>

    </RelativeLayout>

    activity_main_jm

  • 相关阅读:
    Python模块进阶、标准库、扩展库
    python垃圾回收机制
    VMWare workstation 安装 CentOS 8后自适应调整分辨率(如1920x1080)
    使用 Zeal 打造属于自己的文档
    Erlang 开发者的福音:IntelliJ IDEA 的 Erlang 插件
    Intellij IDEA 14的注册码
    在Intellij IDEA或者PhpStorm下用X-debug调试PHP
    PHPCMS 核心代码与 www 分离部署
    PHPCMS如何实现后台访问限制?
    推荐:PHPCMS v9 安全防范教程!
  • 原文地址:https://www.cnblogs.com/zaixiachengxuyuan/p/13111134.html
Copyright © 2020-2023  润新知