Android日期时间操作提供了DatePicker与TimePicker,在布局中可以直接应用,但是这样整个布局空间会被占用很大区域,影响布局美观;而Android这个局限,提供了DatePickerDialog与TimePickerDialog让日期与时间作为弹窗展示,简化了布局占用,下面进入代码:
1.布局文件代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/login_text"
android:layout_width="match_parent"
android:layout_height="60dp"
android:textSize="25sp"
android:gravity="center|bottom"
android:textColor="#f0f0f0"
android:background="#3194d0"
android:text="安卓控件" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@android:color/black"
android:text="设置日期:"
/>
<EditText
android:id="@+id/setting_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2018年11月30日"
android:inputType="none"
android:focusable="false"
android:cursorVisible="true"
android:textCursorDrawable="@null"
android:textSize="20sp"
android:textColor="@android:color/black"
android:background="@null"
/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#90909090" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp">
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:text="设置时间:"
android:textColor="@android:color/black"
android:textSize="20sp" />
<EditText
android:id="@+id/setting_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11:30"
android:inputType="none"
android:focusable="false"
android:cursorVisible="true"
android:textCursorDrawable="@null"
android:textSize="20sp"
android:textColor="@android:color/black"
android:background="@null"
/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#90909090" />
</LinearLayout>
在布局中日期与时间的DatePicker与TimePicker并没有体现,而是使用了EditText元素展示时间。
(1)android:inputType="none"
(2)android:focusable="false"
(3)android:cursorVisible="true"
(4)android:textCursorDrawable="@null"
四个属性设置让EditText不可编辑,不弹出键盘、不显示下划线、不出现光标跳动。当然用其他元素代替EditText也是一样的,不过要注意样式的调整。
2.DatePickerDialog
public class AndroidFragment extends Fragment{
private EditText setting_data;
public AndroidFragment() {
// Required empty public constructor
}@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_android, container, false);
setting_data = view.findViewById(R.id.setting_data);
setting_data.setOnClickListener(new View.OnClickListener(){@Override
public void onClick(View view) {
//得到Calendar实例,可以通过这个实例得到当前年月日时分秒
Calendar c = Calendar.getInstance();
DatePickerDialog datagramPacket = new DatePickerDialog(getContext(),AlertDialog.THEME_HOLO_LIGHT,
new DatePickerDialog.OnDateSetListener(){
//在弹出DatePickerDialog中,点击确定后会触发onDateSet方法
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
//月份是从0开始,所以+1
String select_month = String.valueOf(month+1);
String select_day = String.valueOf(day);
//月份、日小于10,前面加一个0保持2位数显示
if((month+1)<10){
select_month = "0"+(month+1);
}
if(day<10){
select_day = "0"+day;
}
//把选择的日期填写到布局中EditText中
setting_data.setText(year+"年"+select_month+"月"+select_day+"日");
}
},c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH));
datagramPacket.show();
}
});return view;
}
在Fragment中的应用与在Activity中是一样的,没什么太大区别;在初始化DatePickerDialog与TimePickerDialog对象时传入了AlertDialog.THEME_HOLO_LIGHT,这表示日期时间控件的样式,Android提供的样式非常多,不过都大同小异。逻辑也很明显了,就是点击EditText时初始化DatePickerDialog对象,然后在弹窗中显示,最后选择日期回填到EditText中即可。EditText都给了默认值,这个根据需要设置就行了,如果想设置当前时间为默认值,则可以在代码中实现。
3.TimePickerDialog
public class AndroidFragment extends Fragment{
private EditText setting_time;
public AndroidFragment() {
// Required empty public constructor
}@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_android, container, false);
setting_time = view.findViewById(R.id.setting_time);
setting_time.setOnClickListener(new View.OnClickListener(){@Override
public void onClick(View view) {
Calendar c = Calendar.getInstance();
new TimePickerDialog(getContext(),AlertDialog.THEME_HOLO_LIGHT,new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String select_hour = String.valueOf(hourOfDay);
String select_minute = String.valueOf(minute);
if(hourOfDay<10){
select_hour = "0"+hourOfDay;
}
if(minute<10){
select_minute = "0"+minute;
}
setting_time.setText(select_hour+":"+select_minute);
}
}, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true).show();
}
});
return view;
}
}
注意点:
1.如果模拟器默认的语言是英文,在日期时间选择器上会显示出英文的月份,在Setting里面把语言改为中文即可。
2.日期时间的样式可参考下面的文章
DatePickerDialog:https://blog.csdn.net/hizhangyuping/article/details/80336668
TimePickerDialog:https://blog.csdn.net/qq_17009881/article/details/75371406