DatePicker和TimePicker对话框问题

时间:2016-04-15 05:42:30

标签: android datepicker dialog datetimepicker timepicker

当我点击DatePicker时,TimePickerRelativeLayout对话框都没有打开,而调用相应的对话框我正在使用android:onClick="setDate"android:onClick="setTime"

TimePicker

// On clicking Time picker
public void setTime(View v) {

    Calendar mcurrentTime = Calendar.getInstance();
    int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
    int minute = mcurrentTime.get(Calendar.MINUTE);
    TimePickerDialog mTimePicker;
    mTimePicker = new TimePickerDialog(getApplicationContext(), new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
            mHour = selectedHour;
            mMinute = selectedMinute;
            if (selectedMinute < 10) {
                mTime = selectedHour + ":" + "0" + selectedMinute;
            } else {
                mTime = selectedHour + ":" + selectedMinute;
            }
            mTimeText.setText(mTime);
        }
    }, hour, minute, false); // Is 24 hour time
}

的DatePicker

// On clicking Date picker
public void setDate(View v) {

    Calendar mcurrentTime = Calendar.getInstance();
    final int yearr = mcurrentTime.get(Calendar.YEAR);
    final int month = mcurrentTime.get(Calendar.MONTH);
    final int day_of_month = mcurrentTime.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

            monthOfYear++;
            mDay = dayOfMonth;
            mMonth = monthOfYear;
            mYear = year;
            mDate = day_of_month + "/" + month + "/" + yearr;
            mDateText.setText(mDate);

        }
    };
}

我很确定,我在上面的功能中有missed

如下所示,我也在点击:

中使用这些功能
<LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:clickable="true"
            android:onClick="setDate"
            android:id="@+id/date">

            <ImageView
                android:id="@+id/date_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:orientation="vertical"
                android:layout_toRightOf="@id/date_icon"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/date_text"                       
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/date" />

                <TextView
                    android:layout_width="wrap_content"
                    android:id="@+id/set_date"                       
                    android:layout_height="wrap_content"/>

            </LinearLayout>

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:clickable="true"
            android:onClick="setTime"
            android:id="@+id/time">

            <ImageView
                android:id="@+id/time_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:orientation="vertical"
                android:layout_toRightOf="@id/time_icon"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/time_text"                       
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/time" />

                <TextView
                    android:layout_width="wrap_content"
                    android:id="@+id/set_time"
                    android:layout_height="wrap_content"/>

            </LinearLayout>

        </RelativeLayout>

2 个答案:

答案 0 :(得分:0)

试一试。

 <RelativeLayout
            android:layout_width="match_parent"
            android:clickable="true"
            android:onClick="setTime"
            android:layout_height="wrap_content"
            android:id="@+id/time">

答案 1 :(得分:0)

嗨timepicker只是不会出现那样,,,请尝试以下添加此活动...

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case TIME_DIALOG_ID:
            // set time picker as current time
            return new TimePickerDialog(this, timePickerListener, hour, minute, false);

    }
    return null;
}

使用类似的东西来调用对话框

time.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Toast.makeText(context, "Time", Toast.LENGTH_LONG).show();
            whichBox=1;
            showDialog(TIME_DIALOG_ID);
        }
    });

你甚至可以将它放在外面......

   private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minutes) {
        // TODO Auto-generated method stub
        hour   = hourOfDay;
        minute = minutes;
        updateTime(hour,minute);

    }

谢谢