今天看项目,发现项目中用了两行代码就弹出了一个底部的日期选择器就点进去看了一下玩玩
showDialog(AbConstant.DIALOGBOTTOM, mTimeView1);
initWheelDateStart(mTimeView1, mJieShu);
这里解释一下
1.mtimeview1就是我们弹出来的底部布局的view,xml如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/wheel_title"
android:gravity="center"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:paddingTop="2dip" >
<Button
android:id="@+id/cancelBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dark_blue_btn"
android:text="取消"
android:textColor="@color/white" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/okBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dark_green_btn"
android:text="确定"
android:textColor="@color/white" />
LinearLayout>
<LinearLayout
android:id="@+id/mWheelView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/wheel_bg"
android:gravity="center" >
<com.ab.view.wheel.AbWheelView
android:id="@+id/wheelView1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/wheel_bg_1"/>
<com.ab.view.wheel.AbWheelView
android:id="@+id/wheelView2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="5dip"
android:background="@drawable/wheel_bg_1"/>
<com.ab.view.wheel.AbWheelView
android:id="@+id/wheelView3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="5dip"
android:background="@drawable/wheel_bg_1"/>
LinearLayout>
LinearLayout>
2.这里的mJieShu就是我们点击的组件名称,点击这个组件就弹出一个日期选择器。
3.AbConstant.DIALOGBOTTOM我跟了进去看一下,就是一个int类型,用于区分从哪里弹出的。
public static final int DIALOGBOTTOM = 1;
//这个1就是匹配下面这个id的
public void showDialog(int id, View view)
{
if(id == 1)
{
mBottomDialogView = view;
if(mBottomDialog == null)
{
mBottomDialog = new Dialog(this);
setDialogLayoutParams(mBottomDialog, dialogPadding, 80);
}
mBottomDialog.setContentView(mBottomDialogView, new android.widget.LinearLayout.LayoutParams(diaplayWidth - dialogPadding, -2));
showDialog(id);
}
}
4.就是写我们的年月日的选择器了
private void initWheelDateStart(View mDateView, TextView mText) {
String strD = mText.getText().toString();
int year = 0;
int month = 0;
int day = 0;
Calendar calendar = Calendar.getInstance();
if (AbStrUtil.isEmpty(strD)) {
// 年月日时间选择器
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1;
day = calendar.get(Calendar.DATE);
} else {
String[] arr = strD.split("-");
if (arr.length == 3) {
year = Integer.parseInt(arr[0]);
month = Integer.parseInt(arr[1]);
day = Integer.parseInt(arr[2]);
} else {
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1;
day = calendar.get(Calendar.DATE);
}
}
final AbWheelView mWheelViewY = (AbWheelView) mDateView
.findViewById(R.id.wheelView1);
final AbWheelView mWheelViewM = (AbWheelView) mDateView
.findViewById(R.id.wheelView2);
final AbWheelView mWheelViewD = (AbWheelView) mDateView
.findViewById(R.id.wheelView3);
Button okBtn = (Button) mDateView.findViewById(R.id.okBtn);
Button cancelBtn = (Button) mDateView.findViewById(R.id.cancelBtn);
mWheelViewY.setCenterSelectDrawable(this.getResources().getDrawable(
R.drawable.wheel_select));
mWheelViewM.setCenterSelectDrawable(this.getResources().getDrawable(
R.drawable.wheel_select));
mWheelViewD.setCenterSelectDrawable(this.getResources().getDrawable(
R.drawable.wheel_select));
AbWheelUtil.initWheelDatePicker(this, mText, mWheelViewY, mWheelViewM,
mWheelViewD, okBtn, cancelBtn, year, month, day, Constant.YEAR,
120, false);
}
解析一下,前面的都看的懂吧,就是点击textview组件弹出日期选择器看有没有输入日期,如果有输入日期,则弹出来的年月日就是显示我们输入的那个日期,不然就读取现在的年月日转换为int类型填入,AbWheelUtil.initWheelDatePicker(this, mText, mWheelViewY, mWheelViewM,
mWheelViewD, okBtn, cancelBtn, year, month, day, Constant.YEAR,
120, false);
这一句就是最终确定的那句,参数分别是:上下文,点击的组件名,年转盘,月转盘,日转盘,确定按钮,取消按钮,显示的年,显示的月,显示的日,年转盘开始的年份(这里我写的是int 2000)从2000年开始,年转盘终止的年份(开始年份+120)就是2120年结束,不要初始化。
这就是今天看到的东西,以此为记!