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年结束,不要初始化。
这就是今天看到的东西,以此为记!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用DatePickerDialog类来实现Spinner弹出日期选择器的功能。以下是一个示例代码: ``` public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { private Spinner spinner; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner = findViewById(R.id.spinner); textView = findViewById(R.id.text_view); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(this); } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { if (position == 0) { // 如果选中第一项 showDatePickerDialog(); // 弹出日期选择器 } } @Override public void onNothingSelected(AdapterView<?> parent) { } private void showDatePickerDialog() { final Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // 显示选择的日期 textView.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth); } }, year, month, day); datePickerDialog.show(); } } ``` 上述代码中,我们实现了AdapterView.OnItemSelectedListener接口,并在onItemSelected方法中判断是否选中了第一项,如果是,则调用showDatePickerDialog方法弹出日期选择器。 在showDatePickerDialog方法中,我们使用Calendar类获取当前日期,并使用DatePickerDialog类创建日期选择器。在选择日期后,我们在onDateSet回调方法中将选择的日期显示在TextView中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值