我正在尝试在Android
中制作活动策划应用程序。我已经添加了一个日历widget
现在我想要的是每当用户点击添加事件按钮时,它将转到新的Activity
屏幕,并且用户在日历中选择的日期之前的Activity
会被精确复制到文本字段中。任何人都可以帮我这个代码吗?
P.S.-我在主屏幕上添加了两个Fragments
,一个用于日历,另一个用于Buttons
。
提前致谢。
答案 0 :(得分:2)
如果您的意思是DatePicker
,那么通过注册日期选择监听器就可以获得所选日期
datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) {
Log.d("Date", "Year=" + year + " Month=" + (month + 1) + " day=" + dayOfMonth);
}
});
或者您随时可以通过以下方式获得所选日期:
DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth() + 1;
int year = datePicker.getYear();
或者如果您使用的是CalendarView
,那么简单的getDate()
调用就可以了。现在从中提取日,月,年,并通过Intent
如果您正在使用别的东西,请告诉我们。
答案 1 :(得分:2)
// Declaration
private int mDay;
private int mMonth;
private int mYear;
// Creating click listener of button
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Getting values from DatePicker
DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
mDay = datePicker.getDayOfMonth();
mMonth = datePicker.getMonth() + 1;
mYear = datePicker.getYear();
// Here you have to write the code to start new Activity
// If your second activity name is "SecondActivity" then use below code
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
// Put the date values to the intent
intent.putExtra("Day",mDay);
intent.putExtra("Month",mMonth);
intent.putExtra("Year",mYear);
startActivity(intent);
}
});
在第二个Activity
int day;
int month;
int year;
EditText dateEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
dateEditText = (EditText)findViewById(R.id.dateEditText);
Intent intent = getIntent();
if(intent != null){
day = intent.getIntExtra("Day",0);
month = intent.getIntExtra("Month",0);
year = intent.getIntExtra("Year",0);
Calendar calendar = Calendar.getInstance();
calendar.set(2013,5,23);
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM d, yyyy 'at' h:mm a");
dateEditText.setText(format.format(calendar.getTime()));
}
}
修改强>
// If you are using Calendar View
calendarView.setOnDateChangeListener(new OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView arg0, int year, int month,
int date) {
mDay = date;
mYear = year;
mMonth = month;
// Now you can uses year, month date to sent
}
});
// Click Listener of Button in case of CalenderView
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Here you have to write the code to start new Activity
// If your second activity name is "SecondActivity" then use below code
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
// Put the date values to the intent
intent.putExtra("Day",mDay);
intent.putExtra("Month",mMonth);
intent.putExtra("Year",mYear);
startActivity(intent);
}
});