码迷,mamicode.com
首页 > 移动开发 > 详细

Android自带的时间控件的使用

时间:2020-03-10 21:47:28      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:protect   starty   art   content   mep   pre   widget   height   pac   

链接: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>

 

Android自带的时间控件的使用

标签:protect   starty   art   content   mep   pre   widget   height   pac   

原文地址:https://www.cnblogs.com/wskb/p/12458491.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!