DatePicker的小坑

xiaoxiao2021-02-28  91

相信在做项目的时候的大家很容易就遇到了需要设置时间,这时候你也许会找些开源控件,或者会使用Android原生的控件DatePicker和timerPicker。这项目中我主要使用到了DatePicker,网上看了些资料直接拿来就用了,发现挺好用的。但是在测试不同机型的时候会有问题。 其实主要还是对这个api不熟,然后就用了。

先说问题在有些手机下DatePicker会显示这个效果:

在部分手机下回显示这样,

这会导致app显示效果不同。查看一些资料发现网上给出很多的解决办法: 1.网上很多人说是theme问题,修改android:Theme.Holo.Light.NoActionBar这个主题就能变成上面的样式。但是也有很多人说这个是不行的。

2.还有解决方法是将弹框的alertdialog修改为PopupWindow,这个方法目前没尝试,这个应该可以的。 可以参考文章:http://blog.csdn.net/wzsdxs/article/details/52074025

3.查阅写资料就可以知道有更好的解决方式:DatePicker中有属性 datePickerMode:有calendar和spinner两种模式, 相关属性可以看此文章: http://blog.csdn.net/lxk_1993/article/details/51351365 5.0以上手机默认是calendar模式,图1. 为了确保都是图2模式,选择spinner模式。

我在activity中去去点击按钮,弹出日期选择框,然后将日期返回。

public class SetDataActivity extends AppCompatActivity { @InjectView(R.id.bt_starttime) Button btStarttime; @InjectView(R.id.bt_endtime) Button btEndtime; @InjectView(R.id.i_ch_list_screen) ListView iChListScreen; @InjectView(R.id.tv_reset) TextView tvReset; @InjectView(R.id.tv_set) TextView tvSet; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_set_data); ButterKnife.inject(this); WindowManager windowManager = getWindowManager(); Display defaultDisplay = windowManager.getDefaultDisplay(); WindowManager.LayoutParams p = getWindow().getAttributes(); p.width = (int) (defaultDisplay.getWidth() * 1);//设置宽全屏幕 getWindow().setAttributes(p); } @OnClick({R.id.bt_starttime, R.id.bt_endtime,R.id.tv_reset, R.id.tv_set}) public void onClick(View view) { switch (view.getId()) { case R.id.bt_starttime: ChooseDateDialog chooseDateDialog = new ChooseDateDialog(); chooseDateDialog.showDialog(SetDataActivity.this, new ChooseDateDialog.onButtonClick() { @Override public void OnPositive(String date) { btStarttime.setText(date); LogUtils.wyjlog(date + "================="); } @Override public void OnNegative() { } }); break; case R.id.bt_endtime: ChooseDateDialog chooseDateDialog1 = new ChooseDateDialog(); chooseDateDialog1.showDialog(SetDataActivity.this, new ChooseDateDialog.onButtonClick() { @Override public void OnPositive(String date) { btEndtime.setText(date); } @Override public void OnNegative() { } }); break; case R.id.tv_reset: finish(); break; case R.id.tv_set: String starttime = btStarttime.getText().toString().trim(); String endtime = btEndtime.getText().toString().trim(); Intent intent=new Intent(); Bundle bundle=new Bundle(); bundle.putString("starttime", starttime); bundle.putString("endtime", endtime); intent.putExtras(bundle); setResult(41, intent); finish(); break; } } }

activity的xml如下:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent"> </LinearLayout> <LinearLayout android:layout_weight="3" android:layout_width="0dp" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical" > <LinearLayout android:layout_marginTop="20dp" android:layout_marginLeft="5dp" android:layout_marginBottom="5dp" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_marginLeft="10dp" android:text="起始日期" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <LinearLayout android:layout_marginTop="5dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:background="@color/total_fra_back" android:text="2017-05-03" android:id="@+id/bt_starttime" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout> <LinearLayout android:layout_marginTop="20dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginBottom="5dp" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_marginLeft="10dp" android:text="结束日期" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <LinearLayout android:layout_marginTop="5dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:background="@color/total_fra_back" android:text="2017-05-04" android:id="@+id/bt_endtime" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > <View android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff"/> <ListView android:id="@+id/i_ch_list_screen" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:listSelector="#00000000" /> </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/tv_reset" android:layout_width="wrap_content" android:layout_height="42dp" android:layout_weight="1" android:gravity="center" android:text="@string/evidence_reset" android:textColor="#666666" android:textSize="16sp" /> <TextView android:id="@+id/tv_set" android:layout_width="wrap_content" android:layout_height="42dp" android:layout_weight="1" android:gravity="center" android:text="@string/evidence_set" android:textColor="#666666" android:textSize="16sp" /> </LinearLayout> </LinearLayout> </LinearLayout>

这里用接口回调的方式将日期返回到activity中

public class ChooseDateDialog { private static DatePicker datePicker; // private static TimePicker timePicker; private int mYear; private int mMonth; private int mDay; private String date; /* * 接口回调 * */ private onButtonClick mButtonClickCallBack; public interface onButtonClick{ void OnPositive(String date); void OnNegative(); } public void showDialog(Context context,onButtonClick buttonClickCallBack) { View view = View.inflate(context, R.layout.dialog_date_time_layout, null); datePicker = (DatePicker) view.findViewById(R.id.datepicker); // resizePikcer(datePicker); mButtonClickCallBack=buttonClickCallBack; initData(); new AlertDialog.Builder(context).setTitle("请选择日期").setView(view).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String result1="2017-05-03"; date=result1; if (mButtonClickCallBack != null) { mButtonClickCallBack.OnNegative(); } dialog.dismiss(); } }).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); GregorianCalendar gregorianCalendar = new GregorianCalendar(mYear,mMonth,mDay); String result = format.format(gregorianCalendar.getTime()); result=result.substring(0,10); LogUtils.wyjlog("日期 ======:"+date);//2017-06-10 00:00 if (mButtonClickCallBack != null) { mButtonClickCallBack.OnPositive(result); } dialog.dismiss(); } }).create().show(); }; /* * 获取日期 * */ public String getDate() { return date; }; public void initData(){ Calendar calendar = Calendar.getInstance(); mYear = calendar.get(Calendar.YEAR); mMonth = calendar.get(Calendar.MONTH); mDay = calendar.get(Calendar.DAY_OF_MONTH); /* mHour = calendar.get(Calendar.HOUR_OF_DAY); mMinute = calendar.get(Calendar.MINUTE);*/ datePicker.init(mYear, mMonth, mDay, new DatePicker.OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; } }); } }

对话框的xml布局:

<?xml version="1.0" encoding="utf-8"?> <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" android:orientation="vertical"> <!--android:datePickerMode="spinner"--> <DatePicker android:datePickerMode="spinner" android:id="@+id/datepicker" android:calendarViewShown="false" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>

以上就是我自己的一些理解,还有不足之处,请指出。

转载请注明原文地址: https://www.6miu.com/read-57106.html

最新回复(0)