Android 时间选择器
在最近做实验编码过程中,又用之前写的一个题目
前言
因为自己有本书Android的用户界面设计,就感觉之前的ui布局不是特别的符合,就重新写了一些ui界面,然后发现当时对于时间选择器的代码编写,是写在类的方法里面的,如果我要在很多地方都使用时间选择器,就要方法写很多次,就想着可不可以封装起来,也方便以后使用
终于是经过很多次的闪退之后,突然开窍就封装好了
可能其中还是有可以进行优化的部分
不过时间选择器还是单独写在另一个类里面了
简单布局
布局还有一些功能没有时间,例如确定按钮,主要只是说明时间选择器
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:divider="@drawable/line"
app:dividerPadding="5dp"
app:showDividers="end|middle"
tools:context=".ui.MeetingTime">
<androidx.appcompat.widget.Toolbar
android:id="@+id/time_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#FFBB86FC"
app:contentInsetStart="0dp"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|snap">
<ImageView
android:id="@+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/remove"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="会议室预约"
android:textColor="@color/white"
android:textSize="20sp" />
</androidx.appcompat.widget.Toolbar>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="主题"
android:textColor="#000"
android:textSize="21sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:hint="会议主题"
android:paddingLeft="30dp"
android:textSize="@dimen/sp_16"
android:textColorHint="#D3D3D3"
android:imeOptions="actionDone"
android:inputType="text"/>
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="日期"
android:textColor="#000"
android:textSize="21sp" />
<TextView
android:id="@+id/start_day"
android:layout_width="145dp"
android:layout_height="match_parent"
android:background="@null"
android:textSize="18sp"
android:textColor="@color/black"
android:gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="至"
android:textColor="#000"
android:textSize="21sp" />
<TextView
android:id="@+id/end_day"
android:layout_width="145dp"
android:layout_height="match_parent"
android:background="@null"
android:textSize="18sp"
android:textColor="@color/black"
android:gravity="center"/>
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="时间"
android:textColor="#000"
android:textSize="21sp"/>
<TextView
android:id="@+id/start_time"
android:layout_width="145dp"
android:layout_height="match_parent"
android:background="@null"
android:textSize="18sp"
android:textColor="@color/black"
android:gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="至"
android:textColor="#000"
android:textSize="21sp" />
<TextView
android:id="@+id/end_time"
android:layout_width="145dp"
android:layout_height="match_parent"
android:background="@null"
android:textSize="18sp"
android:textColor="@color/black"
android:gravity="center"/>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
我这里选择时间的位置,控件用的都是TextView,是想直接显示时间
活动Activity
public class MeetingTime extends AppCompatActivity implements View.OnClickListener {
private Toolbar toolbar;
private TextView start_day, end_day; //日期
private TextView start_time, end_time; //时间
private TimePickerView temp; //时间选择器对象
TimePicker timePicker = new TimePicker();
private ImageView remove;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meeting_time);
init();
}
private void init() {
toolbar = (Toolbar) findViewById(R.id.time_toolbar);
start_day = (TextView) findViewById(R.id.start_day);
end_day = (TextView) findViewById(R.id.end_day);
start_time = (TextView) findViewById(R.id.start_time);
end_time = (TextView) findViewById(R.id.end_time);
remove = (ImageView) findViewById(R.id.remove);
start_day.setOnClickListener(this);
end_day.setOnClickListener(this);
start_time.setOnClickListener(this);
end_time.setOnClickListener(this);
remove.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_day:
temp = timePicker.initTimePicker(this,start_day);
temp.show();
break;
case R.id.end_day:
temp = timePicker.initTimePicker(this,end_day);
temp.show();
break;
case R.id.start_time:
temp = timePicker.initTimePicker(this,start_time);
temp.show();
break;
case R.id.end_time:
temp = timePicker.initTimePicker(this,end_time);
temp.show();
//timePicker.initTimePicker(end_time);
//timePickerView.show();
break;
case R.id.remove:
finish();
break;
}
}
}
这里代码 很好理解
就是一个标题Toolbar,左上角一个返回的图标,几个TextView,
TextView设置点击事件,点击文本了开始选择时间
然后是一个时间选择器,还有一个封装好的时间选择的类
工具类——时间选择TimePicker
public class TimePicker {
public TimePickerView initTimePicker(Context context, TextView n) {
Calendar selectedDate = Calendar.getInstance();
Calendar startDate = Calendar.getInstance();
//startDate.set(2000,2,11); //起始时间
Calendar endDate = Calendar.getInstance();
endDate.set(2025, 12, 31); //结束时间
TimePickerView timePickerView = new TimePickerView.Builder(context, new TimePickerView.OnTimeSelectListener() {
@Override
public void onTimeSelect(Date date, View v) {
//选中事件回调
n.setText(getTimes(date));
}
})
//年月日时分秒 的显示与否,不设置则默认全部显示
.setType(new boolean[]{true, true, true, false, false, false})
.setLabel("年", "月", "日", "时", "", "")
.isCenterLabel(true)
.setDividerColor(Color.DKGRAY)
.setContentSize(21)
.setDate(selectedDate)
.setRangDate(startDate, endDate)
.setDecorView(null)
.build();
return timePickerView;
}
//格式化时间
public String getTimes(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(date);
}
}
可以看到,里面有两个方法
一个是格式化时间的方法,还有一个就是相当于初始化,设置了很多参数
获取当前日期,起始时间日期就从当前开始,结束时间我设置到了2025年,然后在下面的一个布尔型的数组还可以看到我只是显示了年月日并没有使用时分秒,还有就是比较基础的一些参数
格式化时间就是帮助显示的
传参
最后讲讲传的参数
格式化时间方法传的参数自然是data,这个就不用说
比较主要的就是设置时间选择器的方法,传了一个Context和TextView
一开始呢是作为方法写在需要使用时间选择器的类里面的,
timePickerView = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener()
所以之前的上下文就是当前的活动,就类似于上面实例代码,用this来获取当前活动的上下文,而现在呢是封装在另外一个类里面了,自然要将当前的活动上下文传过去,保证当前活动可以获取到,然后因为我用的是TextView来作为点击然后使用时间选择器,所以我就传了一个TextView
可以根据使用的控件来传,这里就可以算作传参是一个Object类
然后写的时候我还犯了一个比较基础的错误,因为单独封装了,所以要创建对象new一个对象,一开始封装好了,一直闪退,我就感觉很奇怪,这样写不可以嘛?到后面才发现是没有创建对象
//错误
TimePicker timePicker ;
//正确
TimePicker timePicker = new TimePicker();
个人认为封装起来然后在需要使用时间选择器的活动创建对象使用就可以了,就比又在活动里面添加方法比较便捷,也让代码看起来整齐一点
这也是我突发奇想的方法,可能比较简单,也没什么技术含量
不过感觉还是对自己以后使用这方面的知识还是有一个小的练习,也对时间选择器可以理解更加深刻
❤