我使用以下代码从DatePickerDialog
创建EditText
。
import android.widget.DatePicker;
import android.app.DatePickerDialog;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
Calendar myCalendar = Calendar.getInstance();
String myFormat = "MM/dd/yy";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
@Override
protected void onCreate(Bundle savedInstanceState) {
EditText datePicker = (EditText) findViewById(R.id.popUpDate);
datePicker.setText(sdf.format(myCalendar.getTime()));
datePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(AddAccount.this, R.style.AppTheme_Dialog, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
datePicker.setText(sdf.format(myCalendar.getTime()));
}
}, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
选择日期后是否可以自动关闭默认值?或者这需要完全翻拍datepicker
小部件?
答案 0 :(得分:2)
DatePickerDialog包含DatePicker。 DatePicker有OnDateChangedListener 当您选择
之类的日期时,可以使用OnDateChangedListener来监听final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
final DatePickerDialog datePickerDialog =
new DatePickerDialog(YourActivity.this, null, year, month, day);
datePickerDialog.getDatePicker()
.init(year, month, day, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Toast.makeText(YourActivity.this, "day" + dayOfMonth + "-month" + monthOfYear + 1 + "-year" + year, Toast.LENGTH_SHORT).show();
// dismiss the popup when choose a date
datePickerDialog.dismiss();
}
});
datePickerDialog.show();
此解决方案适用于您的情况,但我认为用户体验(UX)不是很好,因为几乎Android应用使用默认的DatePickerDialog流
答案 1 :(得分:2)
创建 MyPicker 类:)
import static yourPackageName.MainActivity.sdf; // get static imports
public class MyPicker extends DatePickerDialog {
Context context;
@Override
public DatePicker getDatePicker() {
return super.getDatePicker();
}
public MyPicker(Context context, int themeResId, OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) {
super(context, themeResId, listener, year, monthOfYear, dayOfMonth);
this.context = context;
}
@Override
public void onDateChanged(DatePicker view, int year, int month, int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, month);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
datePicker.setText(sdf.format(myCalendar.getTime()));
datePickerDialog.dismiss();
}
}
现在假设你的主要课程在 MainActivity
public class MainActivity extends AppCompatActivity {
public static Calendar myCalendar = Calendar.getInstance();
public static String myFormat = "MM/dd/yy";
public static SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
public static EditText datePicker;
public static MyPicker datePickerDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
datePicker = (EditText) findViewById(R.id.popUpDate);
datePicker.setText(sdf.format(myCalendar.getTime()));
datePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
datePickerDialog = new MyPicker(MainActivity.this, R.style.AppTheme_Dialog, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
}
}, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}
});
}
}
}
或者如果您不需要应用样式
public class MainActivity extends AppCompatActivity {
public String myFormat = "MM/dd/yy";
public SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
public EditText datePicker;
final Calendar calendar = Calendar.getInstance();
private DatePickerDialog datePickerDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
datePicker = (EditText) findViewById(R.id.popUpDate);
datePicker.setText(sdf.format(calendar.getTime()));
datePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
datePickerDialog = new DatePickerDialog(MainActivity.this, null, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.getDatePicker().init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
datePicker.setText(sdf.format(calendar.getTime()));
datePickerDialog.dismiss();
}
});
datePickerDialog.show();
}
});
}
}
像魅力一样!
答案 2 :(得分:1)
目前的行为还不错?
如果它会自动关闭......!然后系统将如何知道你选择 所需的日期和时间,或者你不小心选择它......没有 感觉自动关闭它而不是事件火。
但您可以在onDateSet
上关闭它。
更新:您可以这样关闭它。
// open datepicker dialog.
// set date picker for current date
// add pickerListener listner to date picker
DatePickerDialog dialog = new DatePickerDialog(this, pickerListener, year, month,day);
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Text", new DialogInterface
.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_NEGATIVE) {
// Do Stuff
Log.i("dialog click", "dialog negative button clicked");
dialog.dismiss();
}
}
});
dialog.dismiss();
答案 3 :(得分:0)
@Phan,请尝试使用以下代码
DatePickerDialog dialog;
datePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog = new DatePickerDialog(AddAccount.this, R.style.AppTheme_Dialog, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
datePicker.setText(sdf.format(myCalendar.getTime()));
if(dialog != null) {
dialog.dismiss();
}
}
}, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
dialog.show();
}
});