1. 程式人生 > >Android AndBase框架之底部彈出日期選擇器

Android AndBase框架之底部彈出日期選擇器

今天看專案,發現專案中用了兩行程式碼就彈出了一個底部的日期選擇器就點進去看了一下玩玩

showDialog(AbConstant.DIALOGBOTTOM, mTimeView1);
initWheelDateStart(mTimeView1, mJieShu);

這裡解釋一下
1.mtimeview1就是我們彈出來的底部佈局的view,xml如下

<?xml version="1.0" encoding="utf-8"?>
<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年結束,不要初始化。
這就是今天看到的東西,以此為記!