java – autoclose DatePickerDialog
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – autoclose DatePickerDialog,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4266字,纯文字阅读大概需要7分钟。
内容图文
![java – autoclose DatePickerDialog](/upload/InfoBanner/zyjiaocheng/727/488c638852184b74a7178775d6934a22.jpg)
我使用以下代码从EditText创建一个DatePickerDialog.
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小部件?
解决方法:
创建一个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();
}
});
}
}
奇迹般有效!
内容总结
以上是互联网集市为您收集整理的java – autoclose DatePickerDialog全部内容,希望文章能够帮你解决java – autoclose DatePickerDialog所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】