zoukankan      html  css  js  c++  java
  • Android自带的时间控件的使用

    链接:https://www.cnblogs.com/xianfeng-zhang/p/8134269.html

    1,activity代码:

    package com.example.fitness_app.web.teacher;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.DatePicker;
    import android.widget.EditText;
    import android.widget.TimePicker;
    
    import com.example.fitness_app.R;
    
    public class teacher_addcourseActivity extends AppCompatActivity implements DatePicker.OnDateChangedListener {
        private int Year;
        private  int month;
        private  int day;
        private  int hour;
        private  int min;
    
        private  EditText editText = null;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_teacher_addcourse);
    
            //时间控件
            editText = findViewById(R.id.edittime);
            DatePicker datePicker = findViewById(R.id.coursdaydate);
            datePicker.init(Year,month,day,this);
    //        TimePicker timePicker = new TimePicker();

    TimePicker timePicker = findViewById(R.id.coursdaytime); timePicker.setIs24HourView(true); // editText.setText(Year+"-"+month+"-"+day+" "+hour+":"+min); timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { hour = hourOfDay; min = minute; if (minute<10){ hour = hourOfDay; min = minute; Log.i("time","小时"+hour+"分钟"+"0"+min); editText.setText(Year+"-"+month+"-"+day+" "+hour+":"+min); }else { Log.i("time","小时"+hour+"分钟"+min); editText.setText(Year+"-"+month+"-"+day+" "+hour+":"+min); } } }); } @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { month = monthOfYear +1; Year = year; day = dayOfMonth; Log.i("shijian",Year+"年"+month+"月"+day+"日"); editText.setText(Year+"-"+month+"-"+day+" "+hour+":"+min); } }

    2,xml代码

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <DatePicker
                android:id="@+id/coursdaydate"
                android:layout_height="150dp"
                android:layout_width="200dp"
                android:datePickerMode="spinner"
                android:calendarViewShown="false"
                android:startYear="2020"
                android:scrollbarSize="10dp"
                />
            <TimePicker
                android:id="@+id/coursdaytime"
                android:layout_toRightOf="@id/coursdaydate"
                android:layout_width="wrap_content"
                android:layout_height="150dp"
                android:timePickerMode="spinner"></TimePicker>
  • 相关阅读:
    Java连载63-异常处理try...catch...、方法getMessageyu printStackTrace
    Python连载58-http协议简介
    Java连载62-使用throws关键字处理异常
    HTML连载57-相对定位和绝对定位
    Java连载61-异常的机制与分类
    Python连载57- 邮件头和主题、解析邮件
    Java连载60-类之间的六种关系
    [Java] 数据库编程JDBC
    [bug] MySQL-Front连接MySQL 8.0失败
    [bug]mysql: The server time zone value '&#214;&#208;&#185;&#250;&#177;&#234;&#215;&#188;&#202;&#177;&#188;&#228;' is unrecognized or represents more than one time zone
  • 原文地址:https://www.cnblogs.com/wskb/p/12458491.html
Copyright © 2011-2022 走看看