好吧,我正在做一个应用程序,它有一个活动和另一个名为 MyPicker 的类,它扩展了 DialogFragment 并实现了 DatePickerDialog.OnDateSetListener
问题是当用户单击一个按钮时,会出现一个 dataPicker,我想将该日期从 datePicker 带到一个 texfield,它是活动。
你建议我怎么做?
好吧,我正在做一个应用程序,它有一个活动和另一个名为 MyPicker 的类,它扩展了 DialogFragment 并实现了 DatePickerDialog.OnDateSetListener
问题是当用户单击一个按钮时,会出现一个 dataPicker,我想将该日期从 datePicker 带到一个 texfield,它是活动。
你建议我怎么做?
嗨,这个问题已经在stackoverflow上问了很多次了。你可以做这样的事情-
public class MainActivity extends Activity {
TextView textDate;
private int year;
private int month;
private int day;
static final int DATE_DIALOG_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textDate = (TextView) findViewById(R.id.text_date);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
textDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, year, month,
day);
}
return null;
}
// updates the date we display in the TextView
private void updateDisplay() {
/*
* Hide virtual keyboard
*/
textDate.setText(new StringBuilder()
// Month is 0 based so add 1
.append(year).append("-").append(month + 1).append("-")
.append(day).append(""));
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int myear, int monthOfYear,
int dayOfMonth) {
year = myear;
month = monthOfYear;
day = dayOfMonth;
updateDisplay();
}
};
}
演示链接在这里-
public class MainActivity extends Activity implements OnClickListener {
private Button mDateButton;
private Calendar mCalen;
private int day;
private int month;
private int year;
private static final int DATE_PICKER_ID = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDateButton = (Button) findViewById(R.id.date_button);
mCalen = Calendar.getInstance();
day = mCalen.get(Calendar.DAY_OF_MONTH);
month = mCalen.get(Calendar.MONTH);
year = mCalen.get(Calendar.YEAR);
mDateButton.setOnClickListener(this);
}
@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_PICKER_ID:
return new DatePickerDialog(this, datePickerListener,
year, month, day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener =
new DatePickerDialog.OnDateSetListener() {
// while dialog box is closed, below method is called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// Set the Date String in Button
mDateButton.setText(day + " / " + (month + 1) + " / " + year);
}
};
@Override
public void onClick(View v) {
showDialog(DATE_PICKER_ID);
}
}
如果您要更改日期选择器中的日期,那么您可以这样做
datePicker.setOnDateChangeListner(new OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year1,
int monthOfYear1, int dayOfMonth1) {
year = year1;
monthOfYear = monthOfYear1;
dayOfMonth = dayOfMonth1;
}
}
);