1

我正在尝试通过调用 Android 应用程序的两个不同按钮来弹出日期选择器,我能够为 bStartDate 按钮弹出日期选择器,但无法获得 bEndDate 按钮。

我无法确定它们是否应该在不同的文件或相同的文件中实现,或者这段代码可能存在什么问题。

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class StartDateActivity extends Activity{


    static final int DATE_DIALOG_ID = 1;
    static final int DATE_DIALOG_ID_END = 2;
    private int startYear;
    private int startMonth;
    private int startDay;
    private int endYear;
    private int endMonth;
    private int endDay;
    private String month;
    private String dateOfBirth;

    Button bStartDate;
    Button bEndDate;
    Button bPriority;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
    //  Log.d("Some ", "Getting Called");
        super.onCreate(savedInstanceState);
        bStartDate = (Button) findViewById(R.id.bStartDate);
        bEndDate = (Button) findViewById(R.id.bEndDate);
        bPriority = (Button) findViewById(R.id.bPriority);



    bStartDate.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            showDialog(1);
        }   
    });


    bEndDate.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            showDialog(2);
        }
    });

    final Calendar calendar = Calendar.getInstance();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    if (bStartDate.getText().toString() != null) {
        try {
            calendar.setTime(df.parse(bStartDate.getText().toString()));
        } catch (java.text.ParseException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        startYear = calendar.get(Calendar.YEAR);
        startMonth = calendar.get(Calendar.MONTH);
        startDay = calendar.get(Calendar.DAY_OF_MONTH);
        SimpleDateFormat month_date = new SimpleDateFormat("MMM");
        month = month_date.format(calendar.getTime());
    } else {
        startYear = calendar.get(Calendar.YEAR);
        startMonth = calendar.get(Calendar.MONTH);
        startDay = calendar.get(Calendar.DAY_OF_MONTH);
        SimpleDateFormat month_date = new SimpleDateFormat("MMM");
        month = month_date.format(calendar.getTime());
    }


    //End Date Stuff ... 

    final Calendar calendar1 = Calendar.getInstance();
    SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");

    if (bEndDate.getText().toString() != null) {
        try {
            calendar1.setTime(df1.parse(bEndDate.getText().toString()));
        } catch (java.text.ParseException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        endYear = calendar1.get(Calendar.YEAR);
        endMonth = calendar1.get(Calendar.MONTH);
        endDay = calendar1.get(Calendar.DAY_OF_MONTH);
        SimpleDateFormat month_date1 = new SimpleDateFormat("MMM");
        month = month_date1.format(calendar1.getTime());
    } else {
        endYear = calendar1.get(Calendar.YEAR);
        endMonth = calendar1.get(Calendar.MONTH);
        endDay = calendar1.get(Calendar.DAY_OF_MONTH);
        SimpleDateFormat month_date2 = new SimpleDateFormat("MMM");
        month = month_date2.format(calendar1.getTime());
    }

/*
 * 
    if (cal_currentTime.compareTo(calendar) > 0)
    {
        //updateDisplay();
    }
*/
    }





@Override
protected Dialog onCreateDialog(int id) {
   if(id == 1){
        return new DatePickerDialog(this, mDateSetListener, startYear, startMonth,
                startDay);
   }else if(id==2){

         return new DatePickerDialog(this, mDateSetListener2,endYear, endMonth,
                 endDay);
    }
    return null;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
 if(id==1){
    ((DatePickerDialog) dialog).updateDate(startYear, startMonth, startDay);
 }  else if(id==2){
     ((DatePickerDialog) dialog).updateDate(endYear, endMonth, endDay);

    }
}

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

    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        startYear = year;
        startMonth = monthOfYear;
        startDay = dayOfMonth;

        String dateSetter = (new StringBuilder().append(startYear).append("-")
                .append(startMonth + 1).append("-").append(startDay).append(""))
                .toString();
        final Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        if (dateSetter != null) {
            try {
                cal.setTime(df.parse(dateSetter));
            } catch (java.text.ParseException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            SimpleDateFormat month_date = new SimpleDateFormat("MMM");
            month = month_date.format(cal.getTime());
        }
/*
        if (cal_currentTime.compareTo(cal) > 0)
            updateDisplay();
        else
            */
            Toast.makeText(getBaseContext(), "Choose Proper date format",
                    Toast.LENGTH_SHORT).show();
    }
};

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

    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        endYear = year;
        endMonth = monthOfYear;
        endDay = dayOfMonth;

        String dateSetter = (new StringBuilder().append(endYear).append("-")
                .append(endMonth + 1).append("-").append(endDay).append(""))
                .toString();
        final Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        if (dateSetter != null) {
            try {
                cal.setTime(df.parse(dateSetter));
            } catch (java.text.ParseException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            SimpleDateFormat month_date = new SimpleDateFormat("MMM");
            month = month_date.format(cal.getTime());
        }
/*
        if (cal_currentTime.compareTo(cal) > 0)
            updateDisplay();
        else
            */
            Toast.makeText(getBaseContext(), "Choose Proper date format",
                    Toast.LENGTH_SHORT).show();
    }
};


private void updateDisplay() {
    dateOfBirth = (new StringBuilder()
            // Month is 0 based so add 1
            .append(startYear).append("-").append(startMonth + 1).append("-")
            .append(startDay).append("")).toString();

    bStartDate.setText(new StringBuilder()
            // Month is 0 based so add 1
            .append(startDay).append("-").append(month).append("-")
            .append(startYear));

    bEndDate.setText(new StringBuilder()
    // Month is 0 based so add 1
    .append(endDay).append("-").append(month).append("-")
    .append(endYear));
}



}
4

0 回答 0