安卓:使用日期选择器和timepicker从对话框中日期、选择器、对话框中、timepicker

2023-09-06 10:59:36 作者:三年成伤

我现在面临一个问题,我不知道该如何解决? 我有一个活动,当我点击链接到该活动菜单的particuliar项目,将显示一个对话框,用于添加的项目。此产品有一个日期和时间,但我不管理有这个对话中的DatePicker和TimePicker。我也试着传递活动的对话框,用这个来显示日期选择器,但是,这并不正常工作。 在此之前,我经手创作中的另一个活动等项目。在这种情况下,它工作正常。但我发现对话框性感... :-) 请问你有什么想法? 希望我不是太糊涂..... 多谢, 吕克

我编辑这篇文章共享code我有困难。

我有尝试过使用的DatePicker和TimePicker一个基本的对话类。基本上,Eclipse的抱怨:

ShowDialog的是未定义View.OnClickListener() onCreateDialog方法:该方法onCreateDialog(INT)型EventCreateDialog必须覆盖或实现超类型的方法 DatePickerDialog是不确定的(因为这不是一个acitvity)

所有这些东西的作品,从一个活动中,但我不能把它从一个对话框的工作。

非常感谢, 吕克

 包com.android.myapp;
进口 ...
公共类TestDialog扩展对话框实现android.view.View.OnClickListener {
私人TextView的mDateDisplay;
私人按钮mPickDate;
私人按钮mPickTime;
私人诠释mYear;
私人诠释mMonth;
私人诠释MDAY;
私人诠释mHour;
私人诠释mMinute;
静态最终诠释DATE_DIALOG_ID = 0;
静态最终诠释TIME_DIALOG_ID = 1;

私人按钮mButton_ok;

私人按钮mButton_ko;

私人ReadyListener readyListener;

私人上下文的背景下;

    公共TestDialog(上下文的背景下,ReadyListener readyListener){
        超(上下文);
        this.context =背景;
        this.readyListener = readyListener;
    }

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.event_create);

        mButton_ok =(按钮)findViewById(R.id.button_ok);
        mButton_ko =(按钮)findViewById(R.id.button_ko);

        //添加监听器
        mButton_ok.setOnClickListener(本);
        mButton_ko.setOnClickListener(本);

            mDateDisplay =(TextView中)findViewById(R.id.dateDisplay);
            mPickDate =(按钮)findViewById(R.id.pickDate);
            mPickTime =(按钮)findViewById(R.id.pickTime);
            //增加一个点击监听器的按钮
            mPickDate.setOnClickListener(新View.OnClickListener(){
                公共无效的onClick(视图v){的ShowDialog(DATE_DIALOG_ID); }
            });
            mPickTime.setOnClickListener(新View.OnClickListener(){
                公共无效的onClick(视图v){的ShowDialog(TIME_DIALOG_ID); }
            });
            //获取当前日期
            最后的日历C = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            MDAY = c.get(Calendar.DAY_OF_MONTH);
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);
            updateDisplay();
    }

        @覆盖
        受保护的对话框onCreateDialog(INT ID){
            开关(ID){
            案例DATE_DIALOG_ID:
                返回新DatePickerDialog(这一点,mDateSetListener,mYear,mMonth,MDAY);
            案例TIME_DIALOG_ID:
                返回新TimePickerDialog(这一点,mTimeSetListener,mHour,mMinute,假);
            }
            返回null;
        }
        私人无效updateDisplay(){
            mDateDisplay.setText(
                新的StringBuilder()
                    //月份是0基于所以加1
                    .append(mMonth + 1).append( - )
                    .append(MDAY).append( - )
                    .append(mYear).append()
                    .append(垫(mHour))追加。(:)
                   .append(垫(mMinute)));
        }

        //收到回调,当用户设置对话框中的日期
        私人DatePickerDialog.OnDateSetListener mDateSetListener =
            新DatePickerDialog.OnDateSetListener(){
                公共无效onDateSet(DatePicker的观点,年整型,诠释monthOfYear,诠释DAYOFMONTH){
                    mYear =年;
                    mMonth = monthOfYear;
                    MDAY = DAYOFMONTH;
                    updateDisplay();
                }
        };

        私人TimePickerDialog.OnTimeSetListener mTimeSetListener =
            新TimePickerDialog.OnTimeSetListener(){
                公共无效onTimeSet(TimePicker观点,诠释hourOfDay,INT分钟){
                    mHour = hourOfDay;
                    mMinute =分钟;
                    updateDisplay();
                }
        };

        私有静态字符串垫(INT C){
            如果(C> = 10)
                返回将String.valueOf(C);
            其他
                回到0+将String.valueOf(C);
        }

    公共接口ReadyListener {
        公共无效的准备(MyObj中MyObj中);
    }

     @覆盖
        公共无效的onClick(视图v){
        如果(V == mButton_ok){
            //做的东西....

        }


        如果(V == mButton_ko){
            解雇();
        }
     }
}

解决方案

PickerViewSample.java

 包com.sai.samples.views;

进口的java.util.Calendar;

进口android.app.Activity;
进口android.app.DatePickerDialog;
进口android.app.Dialog;
进口android.app.TimePickerDialog;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.DatePicker;
进口android.widget.TextView;
进口android.widget.TimePicker;

公共类PickerViewSample延伸活动{

    静态最终诠释DATE_DIALOG_ID = 1;
    静态最终诠释TIME_DIALOG_ID = 2;
    私人TextView的dateDisplay;
    私人按钮pickDate;
    私人诠释的年,月,日;
    私人TextView的timeDisplay;
    私人按钮pickTime;
    私人诠释小时,分钟;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        dateDisplay =(TextView中)findViewById(R.id.TextView01);
        pickDate =(按钮)findViewById(R.id.Button01);

        pickDate.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                的ShowDialog(DATE_DIALOG_ID);
            }

        });

        最后的日历CAL = Calendar.getInstance();
        年= cal.get(Calendar.YEAR);
        月= cal.get(Calendar.MONTH);
        天= cal.get(Calendar.DAY_OF_MONTH);

        updateDate();

        timeDisplay =(TextView中)findViewById(R.id.TextView02);
        pickTime =(按钮)findViewById(R.id.Button02);

        pickTime.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                的ShowDialog(TIME_DIALOG_ID);

            }

        });

        小时= cal.get(Calendar.HOUR);
        分= cal.get(Calendar.MINUTE);

        录入();
    }

    私人无效录入(){
        。timeDisplay.setText(新的StringBuilder()追加(小时).append(':')
                .append(分));

    }

    私人无效updateDate(){
        dateDisplay.setText(新的StringBuilder()追加(天).append(' - ')
                .append(月+ 1).append(' - ')追加​​(年))。

    }

    私人DatePickerDialog.OnDateSetListener dateListener =
        新DatePickerDialog.OnDateSetListener(){

            @覆盖
            公共无效onDateSet(DatePicker的观点,诠释岁,诠释monthOfYear,
                    INT DAYOFMONTH){
                年=岁;
                月= monthOfYear;
                天= DAYOFMONTH;
                updateDate();
            }
    };

    私人TimePickerDialog.OnTimeSetListener timeListener =
        新TimePickerDialog.OnTimeSetListener(){

            @覆盖
            公共无效onTimeSet(TimePicker观点,诠释hourOfDay,INT分钟){
                小时= hourOfDay;
                分=分钟;
                录入();
            }

    };
    受保护的对话框onCreateDialog(INT ID){
        开关(ID){
        案例DATE_DIALOG_ID:
            返回新DatePickerDialog(这一点,dateListener,年,月,日);
        案例TIME_DIALOG_ID:
            返回新TimePickerDialog(这一点,timeListener,小时,分钟,FALSE);
        }
        返回null;

    }
}
 

main.xml中

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    >
< TextView的Andr​​oid版本:文本=@字符串/ date_text
    机器人:ID =@ + ID / TextView01
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:TEXTSIZE =26px
    机器人:字体=SANS>< / TextView的>
<按钮的android:文本=@字符串/ date_button
    机器人:ID =@ + ID / Button01
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT>< /按钮>

< TextView的Andr​​oid版本:文本=@字符串/ time_text
    机器人:ID =@ + ID / TextView02
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:TEXTSIZE =26px
    机器人:字体=SANS>< / TextView的>
<按钮的android:文本=@字符串/ time_button
    机器人:ID =@ + ID / Button02
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT>< /按钮>
< / LinearLayout中>
 
element ui TimePicker 时间选择器禁用时间

datepickerlayout.xml

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:layout_width =WRAP_CONTENT
  机器人:layout_height =WRAP_CONTENT>
<的DatePicker
    机器人:ID =@ + ID / DatePicker01
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT>< / DatePicker的>
< / LinearLayout中>
 

I am facing a problem I do not know how to solve... I have an activity, when I click on a particuliar item of the menu linked to this activity, a dialog is displayed and used to add an item. This item has a date and a time, but I do not manage to have a DatePicker and TimePicker within this dialog. I also try passing the activity to the dialog and use this one to display the datepicker., but that does not work. Before this, I handled the creation of such items within another Activity. In this case, it works fine. But I found the Dialog sexier... :-) Would you have any ideas ? Hope I am not too confused..... thanks a lot, Luc

I edit this post to share the code I have difficulties with.

I have a basic Dialog class that tried to use DatePicker and TimePicker. Basically, Eclipse complains that:

the showDialog is undefined for View.OnClickListener() onCreateDialog method: The method onCreateDialog(int) of type EventCreateDialog must override or implement a supertype method DatePickerDialog is undefined (as this is not an acitvity)

All this stuff works from within an Activity but I cannot have it working from a Dialog.

Thanks a lot, Luc

 package com.android.myapp;
import ...
public class TestDialog extends Dialog implements android.view.View.OnClickListener{
private TextView mDateDisplay;
private Button mPickDate;
private Button mPickTime;
private int mYear;
private int mMonth;
private int mDay;
private int mHour;
private int mMinute;
static final int DATE_DIALOG_ID = 0;
static final int TIME_DIALOG_ID = 1;

private Button mButton_ok;

private Button mButton_ko;

private ReadyListener readyListener;

private Context context;    

    public TestDialog(Context context, ReadyListener readyListener) {
        super(context);
        this.context = context;
        this.readyListener = readyListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.event_create);          

        mButton_ok = (Button)findViewById(R.id.button_ok);
        mButton_ko = (Button)findViewById(R.id.button_ko);

        // Add listeners
        mButton_ok.setOnClickListener(this);
        mButton_ko.setOnClickListener(this);

            mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
            mPickDate = (Button) findViewById(R.id.pickDate);
            mPickTime = (Button) findViewById(R.id.pickTime);
            // add a click listener to the button
            mPickDate.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) { showDialog(DATE_DIALOG_ID); }
            });
            mPickTime.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) { showDialog(TIME_DIALOG_ID); }
            });
            // get the current date
            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);
            updateDisplay();
    }   

        @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
            case TIME_DIALOG_ID:
                return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false);
            }
            return null;
        }
        private void updateDisplay() {
            mDateDisplay.setText(
                new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mMonth + 1).append("-")
                    .append(mDay).append("-")
                    .append(mYear).append(" ")
                    .append(pad(mHour)).append(":")
                   .append(pad(mMinute)));
        }

        // the callback received when the user "sets" the date in the dialog
        private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay();
                }
        };

        private TimePickerDialog.OnTimeSetListener mTimeSetListener =
            new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    mHour = hourOfDay;
                    mMinute = minute;
                    updateDisplay();
                }
        };

        private static String pad(int c) {
            if (c >= 10)
                return String.valueOf(c);
            else
                return "0" + String.valueOf(c);
        }

    public interface ReadyListener { 
        public void ready(MyObj myObj); 
    } 

     @Override
        public void onClick(View v) {
        if (v == mButton_ok) {
            // Do stuff....

        }


        if(v == mButton_ko){
            dismiss();
        }
     }
}

解决方案

PickerViewSample.java

package com.sai.samples.views;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class PickerViewSample extends Activity {

    static final int DATE_DIALOG_ID = 1;
    static final int TIME_DIALOG_ID = 2;
    private TextView dateDisplay;
    private Button pickDate;
    private int year, month, day;
    private TextView timeDisplay;
    private Button pickTime;
    private int hours, min;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        dateDisplay = (TextView)findViewById(R.id.TextView01);
        pickDate = (Button)findViewById(R.id.Button01);

        pickDate.setOnClickListener( new OnClickListener() {

            @Override
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }

        });

        final Calendar cal = Calendar.getInstance();
        year = cal.get(Calendar.YEAR);
        month = cal.get(Calendar.MONTH);
        day = cal.get(Calendar.DAY_OF_MONTH);

        updateDate();

        timeDisplay = (TextView)findViewById(R.id.TextView02);
        pickTime = (Button)findViewById(R.id.Button02);

        pickTime.setOnClickListener( new OnClickListener () {

            @Override
            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID);

            }

        });

        hours = cal.get(Calendar.HOUR);
        min = cal.get(Calendar.MINUTE);

        updateTime();
    }

    private void updateTime() {
        timeDisplay.setText(new StringBuilder().append(hours).append(':')
                .append(min));

    }

    private void updateDate() {
        dateDisplay.setText(new StringBuilder().append(day).append('-')
                .append(month + 1).append('-').append(year));

    }

    private DatePickerDialog.OnDateSetListener dateListener = 
        new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int yr, int monthOfYear,
                    int dayOfMonth) {
                year = yr;
                month = monthOfYear;
                day = dayOfMonth;
                updateDate();
            }
    };

    private TimePickerDialog.OnTimeSetListener timeListener = 
        new TimePickerDialog.OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                hours = hourOfDay;
                min = minute;
                updateTime();
            }

    };
    protected Dialog onCreateDialog(int id){
        switch(id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, dateListener, year, month, day);
        case TIME_DIALOG_ID:
            return new TimePickerDialog(this, timeListener, hours, min, false);
        }
        return null;

    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView android:text="@string/date_text" 
    android:id="@+id/TextView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="26px" 
    android:typeface="sans"></TextView>
<Button android:text="@string/date_button" 
    android:id="@+id/Button01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"></Button>

<TextView android:text="@string/time_text" 
    android:id="@+id/TextView02" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:textSize="26px" 
    android:typeface="sans"></TextView>
<Button android:text="@string/time_button" 
    android:id="@+id/Button02" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"></Button>
</LinearLayout>

datepickerlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
<DatePicker 
    android:id="@+id/DatePicker01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"></DatePicker>
</LinearLayout>