Android自带的时间控件的使用
Posted 千里之外kb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>
以上是关于Android自带的时间控件的使用的主要内容,如果未能解决你的问题,请参考以下文章