如何将结果传递给我的MainActivity?

时间:2013-07-01 11:26:39

标签: android android-datepicker android-timepicker

我想在我当前的android项目中创建日期和时间选择器。我的MainActivity中有其他代码。这是我添加的新类“Changedatetime”,其中包含我的time和datepicker代码。现在我如何将拣货员的结果传递给我的MainActivity?

Changedatetime:

package com.app.annoy;

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 Changetimedate extends Activity {

    static final int DATE_DIALOG_ID = 1;
    static final int TIME_DIALOG_ID = 2;

    private Button pickDate;
    private int year, month, day;

    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.create_layout);


        pickDate = (Button)findViewById(R.id.date_button);

        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();


        pickTime = (Button)findViewById(R.id.time_button);

        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() {
        pickTime.setText(new StringBuilder().append(hours).append(':')
                .append(min));

    }

    private void updateDate() {
        pickDate.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;

    }
}

1 个答案:

答案 0 :(得分:0)

我不知道这是否会导致您的问题,但您在xml布局中有一些wron声明。在你的第一个按钮中,你写了layout_toLeftOf =“@ + id ....”,删除“+”。在第二个按钮中,您已将ID定义为“@id / ...”,请在此处添加“+”。布局必须如下所示:

    <Button
        android:id="@+id/date_button"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/edit_venue"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/time_button"  <-----this was wrong
        android:text="@string/date_label"
         />


    <Button
        android:id="@+id/time_button"  <---this was wrong
        android:layout_width="160dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/edit_venue"
        android:layout_alignParentRight="true"
        android:text="@string/time_label"
        />