将datepicker中的日期从一个活动传递到另一个活动

时间:2015-01-06 07:26:34

标签: android datepicker

我有一个主要活动,其中包含完美运作的日期选择器 我想将主活动中选择的日期发送到第二个活动,这个活动基本上只是通过获取意图如何在代码下面进行itred但它没有工作

主要活动

package com.Blog.blogname;

import java.util.Calendar;
import java.util.StringTokenizer;

//import org.example.androidsdk.demo.DetailActivity;
//import org.example.androidsdk.demo.MainActivity;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

public class Datepicker extends Activity {
private EditText txtView;
private String initialDate;
private String initialMonth;
private String initialYear;
private DatePickerDialog dialog = null;
Context context;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.datepicker);
    Button btn = (Button) findViewById(R.id.button1);
    txtView = (EditText) findViewById(R.id.textView1);
    context = getApplicationContext();
    btn.setOnClickListener(new View.OnClickListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            Calendar dtTxt = null;

          String preExistingDate = (String) txtView.getText().toString();

          if(preExistingDate != null && !preExistingDate.equals("")){
              StringTokenizer st = new StringTokenizer(preExistingDate,"/");
                  initialMonth = st.nextToken();
                  initialDate = st.nextToken();
                  initialYear = st.nextToken();
                  if(dialog == null)
                  dialog = new DatePickerDialog(v.getContext(),
                                   new PickDate(),Integer.parseInt(initialYear),
                                   Integer.parseInt(initialMonth),
                                   Integer.parseInt(initialDate));
                  dialog.updateDate(Integer.parseInt(initialYear),
                                   Integer.parseInt(initialMonth),
                                   Integer.parseInt(initialDate));

          } else {
              dtTxt = Calendar.getInstance();
              if(dialog == null)
              dialog = new DatePickerDialog(v.getContext(),new PickDate(),dtTxt.getTime().getYear(),dtTxt.getTime().getMonth(),
                                                  dtTxt.getTime().getDay());
              dialog.updateDate(dtTxt.getTime().getYear(),dtTxt.getTime().getMonth(),
                                                  dtTxt.getTime().getDay());
          }

            dialog.show();
        }

    });
}
private class PickDate implements DatePickerDialog.OnDateSetListener {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {

        int monthOfYear1 = monthOfYear + 1;
        String formattedMonth = "" + monthOfYear1;
        String formattedDayOfMonth = "" + dayOfMonth;

        if(monthOfYear1 < 10){

            formattedMonth = "0" + monthOfYear1;
        }
        if(dayOfMonth < 10){

            formattedDayOfMonth = "0" + dayOfMonth;
        }


       // view.updateDate(year, monthOfYear, dayOfMonth);
       // txtView.setText(year+"/"+monthOfYear+"/"+dayOfMonth);

         //view.updateDate(year, formattedMonth, formattedDayOfMonth);
        txtView.setText(year+"-"+formattedMonth+"-"+formattedDayOfMonth);

         Intent intent = new Intent(Datepicker.this, SplashActivityDatewiseCurrentAffairs.class);
         intent.putExtra("Date", year+"-"+formattedMonth+"-"+formattedDayOfMonth);  
        dialog.hide();
    }

}
}

第二项活动

Intent i = getIntent();
String Date = i.getStringExtra("Date");

1 个答案:

答案 0 :(得分:1)

我想你忘了打电话给startActivity(意图)