Bigkoo / Android-PickerView

This is a picker view for android , support linkage effect, timepicker and optionspicker.(时间选择器、省市区三级联动)
Apache License 2.0
13.44k stars 3.45k forks source link

put buttons on bottom of dialog 把按钮放在对话框的底部 #607

Closed abiemann closed 6 years ago

abiemann commented 6 years ago

How to put buttons on bottom of dialog ? 如何在对话框底部放置按钮?

xiaosong520 commented 6 years ago

@abiemann Hi, The following sample code you can be referred to :

 //The first parameter is your layout resouce id.
.setLayoutRes(R.layout.pickerview_custom_options, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                       // do somthing
                    }
                }).build();

In addition, The sample project can also be referred to .The link address: MainActivity

351st lines , initCustomOptionPicker()

In the custom layout, the view of id: optionspicker or timepicker, and its child view can not modify their id, otherwise the NullPointerException will be reported.

abiemann commented 6 years ago

Very good. I used the following code:

` public void showDatePicker(Calendar initialDate) { int year; if (initialDate == null) { initialDate = Calendar.getInstance(); year = (initialDate.get(Calendar.YEAR)) - 32; int month = 0;// January int day = 1; initialDate.set(year, month, day); }

    Calendar startDate = Calendar.getInstance();
    year = (startDate.get(Calendar.YEAR)) - 80;
    startDate.set(Calendar.YEAR, year);

    Calendar endDate = Calendar.getInstance();
    year = (endDate.get(Calendar.YEAR)) - 18;
    endDate.set(Calendar.YEAR, year);

    OnTimeSelectListener listener = (date, v) -> datePicked(Utils.toCalendar(date));

    TimePickerBuilder timePickerBuilder = new TimePickerBuilder(this, listener);
    timePickerBuilder.setType(new boolean[]{true, true, true, false, false, false});
    timePickerBuilder.setCancelText("Cancel");
    timePickerBuilder.setSubmitText("Ok");
    timePickerBuilder.setOutSideCancelable(false);
    timePickerBuilder.isCyclic(false);
    timePickerBuilder.setSubmitColor(getResources().getColor(R.color.colorAccent));
    timePickerBuilder.setCancelColor(getResources().getColor(R.color.colorAccent));
    timePickerBuilder.setTitleBgColor(Color.WHITE);
    timePickerBuilder.setBgColor(Color.WHITE);
    timePickerBuilder.setDividerColor(getResources().getColor(R.color.colorAccent));
    timePickerBuilder.setDate(initialDate);
    timePickerBuilder.setRangDate(startDate, endDate);
    timePickerBuilder.setLabel(" Year", " Month", " Day", "", "", "");
    timePickerBuilder.isCenterLabel(false);
    timePickerBuilder.isDialog(true);
    // text offset: Month, Day, Year affected by (2nd param, 1st param, 3rd param)
    timePickerBuilder.setTextXOffset(0, 10, -10, 0, 0, 0);
    timePickerBuilder.setLayoutRes(R.layout.datepicker, new CustomListener() {

        @Override
        public void customLayout(View v) {
            final Button btnSubmit = v.findViewById(R.id.btnSubmit);
            final Button btnCancel = v.findViewById(R.id.btnCancel);
            btnSubmit.setOnClickListener(v1 -> {
                pvTime.returnData();
                pvTime.dismiss();
            });
            btnCancel.setOnClickListener(v12 -> pvTime.dismiss());
        }
    });
    pvTime = timePickerBuilder.build();
    pvTime.show();
}`

and this xml for custom layout:

`<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">

<TextView
    android:id="@+id/tvTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textColor="@color/pickerview_topbar_title"
    android:textSize="@dimen/pickerview_topbar_title_textsize" />

<LinearLayout
    android:id="@+id/timepicker"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="horizontal"
    android:weightSum="2.7">

    <com.contrarywind.view.WheelView
        android:id="@+id/month"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.9" />

    <com.contrarywind.view.WheelView
        android:id="@+id/day"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.8" />

    <com.contrarywind.view.WheelView
        android:id="@+id/year"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <com.contrarywind.view.WheelView
        android:id="@+id/hour"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"/>

    <com.contrarywind.view.WheelView
        android:id="@+id/min"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <com.contrarywind.view.WheelView
        android:id="@+id/second"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />
</LinearLayout>

<RelativeLayout
    android:id="@+id/rv_topbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/pickerview_bg_topbar">

    <Button
        android:id="@+id/btnCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:gravity="left|center_vertical"
        android:paddingLeft="@dimen/pickerview_topbar_padding"
        android:text="@string/pickerview_cancel"
        android:textColor="@drawable/selector_pickerview_btn"
        android:textSize="@dimen/pickerview_topbar_btn_textsize" />

    <Button
        android:id="@id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@android:color/transparent"
        android:gravity="right|center_vertical"
        android:paddingRight="@dimen/pickerview_topbar_padding"
        android:text="@string/pickerview_submit"
        android:textColor="@drawable/selector_pickerview_btn"
        android:textSize="@dimen/pickerview_topbar_btn_textsize" />
</RelativeLayout>

`