对话时间选择器

时间:2013-12-17 05:38:53

标签: android dialog timepicker

我需要一些关于时间选择器对话框的帮助。这是代码

static final int dialog_id = 0;
int hour,minute;
Button date;
Button time;
DatePicker dp;
TimePicker tp;
EditText et;
EditText et1;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    date = (Button) findViewById(R.id.button1);
    time = (Button) findViewById(R.id.button2);
    et = (EditText) findViewById(R.id.editText1);
    et1 = (EditText) findViewById(R.id.EditText01);
    showDialog(dialog_id);
}

    protected Dialog onCreateDialogDialog(int id)
    {
        switch(id)
        {
            case dialog_id:
            return new TimePickerDialog(this, mTimeSetListener, hour, minute, false);
        }
        return null;
    }

    et.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            date();

        }
    });

    et1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            TimePickerDialog.OnTimeSetListener mTimeSetListener=
            new TimePickerDialog.OnTimeSetListener() {
                @Override

                public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) {
                     hour = selectedHour;
                     minute = selectedMinute;
                    et1.setText( hour + ":" + minute);
                }
            };


        }
    });

为mTimeSetListener的timepicker创建对话框时返回new时出错。 这来自一个教程,但它给我发了一个错误..

2 个答案:

答案 0 :(得分:0)

您的mTimeSetListener应该在onClickListener之外,否则TimePickerDialog无法看到<{1}}

答案 1 :(得分:0)

将mTimeSetListener放在onClickListener之外,因为可以看到TimePickerDialog,并记得下次完整发布错误详情...